[R] subset of data frame

arun smartpink111 at yahoo.com
Sun Feb 9 04:46:56 CET 2014


Hi,
Try:
x[x$Species%in%names(sp)[1:2],]
A.K.




On Saturday, February 8, 2014 10:21 PM, Yuanzhi Li <Yuanzhi.Li at USherbrooke.ca> wrote:
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!

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.





More information about the R-help mailing list