Skip to content
Prev 172224 / 398506 Next

R-code help for filtering with for loop

The apply function which can work on either a row-wise of column-wise  
basis can be used with max  and ">" can return a logical vector that  
will let you separate the rows into those with and without a maximum  
greater than 60.

 > datax <- matrix(rnorm(300)*30,nrow=50)
 > datax <- as.data.frame(datax)

datax[ apply(datax, 1 ,max) >= 64, ]  # the rows from datax with any  
value greater than 64
datax[ apply(datax, 1 ,max) < 64, ]   # the other rows

I am not sure what you mean by a table, since in R "table" generally  
means a contingency table. If you wanted a vector of row numbers, this  
might help:

  which(apply(datax,1,max) > 60)  # [1]  7 17 22 25 29 46 49