Re: [R] Deleting rows satisfying a certain condition (sum of some colums>2)

From: jim holtman <jholtman_at_gmail.com>
Date: Sat, 08 Mar 2008 09:08:58 -0500

Here is a way of doing it. You check to see which ones meet the criteria and then delete them.

> # create indices of columns to match
> first.col <- seq(1, to=ncol(SS) - 2)
> sec.col <- seq(3, to=ncol(SS))
>
> # determine rows that met criteria
> meet.crit <- apply(SS, 1, function(.row) any((.row[first.col] + .row[sec.col]) > 2))
>
> cbind(SS, meet.crit) # show which ones meet criteria

              meet.crit
 [1,] 2 2 2 2         1
 [2,] 2 2 1 2         1
 [3,] 2 2 1 1         1
 [4,] 2 2 0 2         1
 [5,] 2 2 0 1         1
 [6,] 2 2 0 0         0
 [7,] 1 2 2 2         1
 [8,] 1 2 1 2         1
 [9,] 1 2 1 1         1
[10,] 1 2 0 2         1
[11,] 1 2 0 1         1
[12,] 1 2 0 0         0
[13,] 1 1 2 2         1
[14,] 1 1 1 2         1
[15,] 1 1 1 1         0
[16,] 1 1 0 2         1
[17,] 1 1 0 1         0
[18,] 1 1 0 0         0

>
> SS[!meet.crit,] # remove them

     [,1] [,2] [,3] [,4]

[1,]    2    2    0    0
[2,]    1    2    0    0
[3,]    1    1    1    1
[4,]    1    1    0    1
[5,]    1    1    0    0

>
>

On Sat, Mar 8, 2008 at 12:17 AM, Katie C <katie.cheon_at_gmail.com> wrote:
>
> I have a huge matrix and need to delete certain rows. What I need to do is:
> 1.In each row, calculate the sum of jth column and (J+2)th column
> 2. If the sum is greater than 2 then that row needs to be deleted.
>
> I have a sample matrix and my codes here. It does remove some rows but when
> it does, it skips the next row and each time it deletes a row, the dimension
> changes so it gets out of bound. I tried to fix those problems but it did
> not work. Any suggestions please? Thank you.
>
>
> S1=c(2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,
> 1,1,1,1,1,1,2,1,1,0,0,0,2,1,1,0,0,0,2,1,1,0,0,0,2,2,1,2,1,0,2,2,1,2,1,0,2,2,1,2,1,0);
>
> SS=matrix(S1,nrow=18);
>
> nr =dim(SS)[1];
> nc =dim(SS)[2]/2;
> nt=2;
> ms=2;
>
>
> for (i in 1:nr){
> for (j in 1:nc){
> #print(paste("Sum=",SS[i,j]+SS[i,j+nt]," ms=",ms));
> if (SS[i,j]+SS[i,j+nt]> ms){
> SS=SS[-i,]
> nr=dim(SS)[1] #this doesn't update nr in the outer for loop. why?
> print(nr) }
> }
> # i=i-1 #this doesn't help to avoid skipping rows
> }
> SS;
>
> --
> View this message in context: http://www.nabble.com/Deleting-rows-satisfying-a-certain-condition-%28sum-of-some-colums%3E2%29-tp15911408p15911408.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem you are trying to solve?

______________________________________________
R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Received on Sat 08 Mar 2008 - 14:22:55 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Sat 08 Mar 2008 - 14:30:19 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive