[R] subset of data frame

Yuanzhi Li Yuanzhi.Li at USherbrooke.ca
Sun Feb 9 01:03:07 CET 2014


Hi, everyone

I met a small problem when I want take a subset from a data frame. The 
data frame(x) looks like the followings(10 species with 3 measured 
traits):

Species     trait1    trait2     trait3
sp1
sp1
...
sp2
sp2
...

sp10
sp10
...

It would be easy if we want trait values for most abundant species, we 
can do that with the following codes

sp<-table(x$Species)
x[x$Species==names(sp)[1],]

but why it doesn't work if we want to select traits values for more than 
one species(most abundant two species) with similar codes

x[x$Species==names(sp)[1:2],]

And there must be other solutions for this problem. Looking forward for 
your help.

Thanks in advance!




More information about the R-help mailing list