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

From: Katie C <katie.cheon_at_gmail.com>
Date: Fri, 07 Mar 2008 21:17:35 -0800 (PST)

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.
Received on Sat 08 Mar 2008 - 05:24:22 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 - 15: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