Skip to content
Prev 58574 / 398503 Next

Resources for optimizing code

Have you tried reading the manual "An Introduction to R", with special 
attention to "Array Indexing" (indexing for data frames is pretty similar 
to indexing for matrices).

Unless I'm misunderstanding, what you want to do is very simple.  It is 
possible to use numeric vectors with 0 and 1 to indicate whether you want 
to keep the row, but it's a little easier with logical vectors.  Here's an 
example:

 > x <- data.frame(a=1:5,b=letters[1:5])
 > keep.num <- ifelse(x$a %% 2 == 1, 1, 0)
 > keep.num
[1] 1 0 1 0 1
 > keep.logical <- (x$a %% 2) == 1
 > keep.logical
[1]  TRUE FALSE  TRUE FALSE  TRUE
 > x[keep.num==1,,drop=F]
   a b
1 1 a
3 3 c
5 5 e
 > x[keep.logical,,drop=F]
   a b
1 1 a
3 3 c
5 5 e
 >
At Friday 10:34 AM 11/5/2004, Janet Elise Rosenbaum wrote: