[R] subset of data frame

Rui Barradas ruipbarradas at sapo.pt
Sun Feb 9 10:38:07 CET 2014


Hello,

First of all use ?dput to post a data example.

dput(head(x, 20))  # paste the output of this in a post


Now, without a reproducible example it's difficult to say but maybe ?%in%

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

Hope this helps,

Rui Barradas

Em 09-02-2014 00:03, Yuanzhi Li escreveu:
> 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