[R] Subseting by more than one factor...
Fernando Henrique Ferraz Pereira da Rosa
mentus at gmx.de
Thu Jun 19 22:37:03 CEST 2003
Is it possible in R to subset a dataframe by more than one factor, all at
once?
For instance, I have the dataframe:
>data
p1 p2 p3 p4 p5 p6 p7 p8 p9 p10 pred
1 0 1 0 0 0 0 0 0 0 0 0.5862069
4 0 0 0 0 0 0 0 0 0 1 0.5862069
5 0 0 0 0 0 0 1 0 0 0 0.5862069
6 0 0 0 0 0 0 0 1 0 0 0.5862069
7 0 0 1 0 0 0 0 0 0 0 0.5862069
9 0 0 0 0 1 0 0 0 0 0 0.5862069
20 0 1 1 0 0 0 0 0 0 0 0.5862069
22 0 1 0 0 1 0 0 0 0 0 0.5862069
24 0 1 0 0 0 0 1 0 0 0 0.5862069
25 0 1 0 0 0 0 0 1 0 0 0.5862069
27 0 1 0 0 0 0 0 0 0 1 0.5862069
If I want to subset only those points that have p4 = 1, I do:
> subset(data,p4 == 1)
And that's fine. Now suppose I want to subset those that not only have p4
= 1, but also p6 = 1.
I tried subset(data,p4 == 1 && p6 == 1) or subset(data,p4==1 & p6==1).
But it didn't work.
Then I found a clumsy way to do it :
subset(subset(data,p4==1),p6==1)
Which works. But it soon gets very clumsy as the number of conditions
increase (I end up with a really large number of nested subsets). Is there a
simpler way to do that?
--
More information about the R-help
mailing list