Skip to content

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

2 messages · Katie C, jim holtman

#
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;
#
Here is a way of doing it.  You check to see which ones meet the
criteria and then delete them.
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
[,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: