[R] Select subset of data

Phil Spector spector at stat.berkeley.edu
Tue Mar 29 22:28:57 CEST 2011


Lisa -
    Suppose your data frame is called "somedat".  Then

do.call(rbind,spl[sapply(spl,function(z)z$result[1] == 0 & z$result[2] == 0 & sum(z$result) == 1)])

    should give you what you want.

 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu


On Tue, 29 Mar 2011, 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 
>
> Can anybody please help how to get this done? Your help would be greatly
> appreciated. 
>
> Lisa
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Select-subset-of-data-tp3416012p3416012.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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