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.
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.
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
Thank you for your kind help. Your R code works very well. -- View this message in context: http://r.789695.n4.nabble.com/Select-subset-of-data-tp3416012p3416307.html Sent from the R help mailing list archive at Nabble.com.
Windows 7 R 2.12.2 When I launch R, I get the following error message: Fatal error: unable to restore saved dat in .RData after which R closes. What does the error message mean, and what can I do to get R to work? I tried removing the R program, re-downloaded the program, and re-installed the program. I still get the error message. Thanks, John John David Sorkin M.D., Ph.D. Chief, Biostatistics and Informatics University of Maryland School of Medicine Division of Gerontology Baltimore VA Medical Center 10 North Greene Street GRECC (BT/18/GR) Baltimore, MD 21201-1524 (Phone) 410-605-7119 (Fax) 410-605-7913 (Please call phone number above prior to faxing) Confidentiality Statement: This email message, including any attachments, is for th...{{dropped:6}}