[R] Select subset of data

David Winsemius dwinsemius at comcast.net
Tue Mar 29 22:36:35 CEST 2011


On Mar 29, 2011, at 3:40 PM, Lisa wrote:

> Dear All,
>
> I have a dataset that looks like this:
>
>   group subject result v4 v5
> 1      1       1      0  1  0
> 2      1       2      1  0  0
> 3      1       3      0  0  0
> 4      1       4      1  0  0
> 5      2       1      0  1  1
> 6      2       2      0  0  1
> 7      2       3      0  1  1
> 8      3       1      0  1  0
> 9      3       2      0  0  1
> 10     3       3      1  0  0
> 11     3       4      0  1  0
> 12     4       1      1  0  0
> 13     4       2      1  1  0
> 14     4       3      0  0  1
> 15     4       4      0  0  0
> 16     4       5      1  0  1
> ……
>
> I only show 4 groups here. There are several subjects within each  
> group. I
> want to select some groups in which for the firs two subjects, the  
> results
> are equal to 0, and for the other subjects, only one has the result  
> being
> equal to 1. So, for the data above, only the group 3 satisfies these
> conditions. Therefore, the new dataset is:
>
>   group subject result v4 v5
> 8       3         1      0    1  0
> 9       3         2      0    0  1
> 10     3         3      1    0  0
> 11     3         4      0    1  0

 > dfrm$sel <- ave(dfrm$result, dfrm$group, FUN=function(x) x[1]==0  
&x[2]==0 & sum(x[3:length(x)]==1)==1)
 > dfrm$sel
  [1] 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0
 > dfrm
    group subject result v4 v5 sel
1      1       1      0  1  0   0
2      1       2      1  0  0   0
3      1       3      0  0  0   0
4      1       4      1  0  0   0
5      2       1      0  1  1   0
6      2       2      0  0  1   0
7      2       3      0  1  1   0
8      3       1      0  1  0   1
9      3       2      0  0  1   1
10     3       3      1  0  0   1
11     3       4      0  1  0   1
12     4       1      1  0  0   0
13     4       2      1  1  0   0
14     4       3      0  0  1   0
15     4       4      0  0  0   0
16     4       5      1  0  1   0

Using `subset` should be trivial from this point.

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list