views:

186

answers:

2

Hi folks,

I am using R and need to select rows with aged (age of death) less than or equal to laclen (lactation length). I am trying to create a new data frame to only include rows/ids whereby the value of column'aged' is less than its corresponding 'laclength' value.

df:
 id1   id2    laclen    aged
9830  64526    26       6 
7609  64547    28       0 
9925  64551     3       0 
9922  64551     3       5 
9916  64551     3       8 
9917  64551     3       8 
9914  64551     3       2 

the new data frame should look like this:

dfnew: id1 id2 laclen aged 9830 64526 26 6 7609 64547 28 0 9925 64551 3 0 9914 64551 3 2

Any help would be appreciated!

Bazon

+4  A: 
df[df$aged <= df$laclen, ] 

Should the trick. The square brackets allow you to index based on a logical expression.

wkmor1
Thank you so much wkmor1. That really did the trick.
Bazon
thanks, aL3xa! I will keep this one as well. I can see that its very similar to one wkmor1 sent earlier.
Bazon
@aL3xa `attach` without `detach` could be dangerous... And I think that comma is misplaced.
Marek
@Marek, thanks for suggestions! I've added `detach` and placed comma after the right bracket, so it goes like this: `attach(df); newdf <- df[which(aged <= laclen), ]; detach(df)`
aL3xa
@aL3xa You could also use `with` - `newdf <- df[with(df,which(aged <= laclen)), ]` instead of `attach/detach`.
Marek
+5  A: 

You can also do

subset(df, aged <= laclen)
Jonathan Chang
thanks Jonathan!
Bazon
nice one, makes code neater according to me, pity `R CMD check` does not recognize the fields used in the test as legitimate variables. it emits a `NOTE` "no visible binding for global variable".
mariotomo