Dear R users, I know how to select a subset of a data.frame or a matrix by setting some criteria, but this time, I have a long index of cases not really related to 1 or 2 criteria. And I cannot find a simple way to select cases from another data.frame based on this index (sublst in this example).> sublst<-unique(g[comp.lst[[2]],]$Subject) >sublst[1] 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 [30] 50 51 52 53 54 55 56 57 58 59 60 62 63 64> foo.lst <- subset(gTrialList,(TrialList==1 & Subject==sublst))Error: longer object length is not a multiple of shorter object length> foo.lst <- subset(gTrialList,(TrialList==1)) > foo.lst[foo.lst$Subject==sublst]Error: In foo.lst$Subject == sublst : longer object length is not a multiple of shorter object length The only way I know is to use a for-loop to exhaust the index and then combine the cases back together. But will there be a simpler way to do so? Thank you very much for the help! - John
?"%in%" Bert Gunter Genentech Nonclinical Biostatistics -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of tsunhin wong Sent: Wednesday, June 03, 2009 3:21 PM To: r-help at r-project.org Subject: [R] Select cases of data.frame based on a long index Dear R users, I know how to select a subset of a data.frame or a matrix by setting some criteria, but this time, I have a long index of cases not really related to 1 or 2 criteria. And I cannot find a simple way to select cases from another data.frame based on this index (sublst in this example).> sublst<-unique(g[comp.lst[[2]],]$Subject) >sublst[1] 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42 43 44 45 46 47 48 49 [30] 50 51 52 53 54 55 56 57 58 59 60 62 63 64> foo.lst <- subset(gTrialList,(TrialList==1 & Subject==sublst))Error: longer object length is not a multiple of shorter object length> foo.lst <- subset(gTrialList,(TrialList==1)) > foo.lst[foo.lst$Subject==sublst]Error: In foo.lst$Subject == sublst : longer object length is not a multiple of shorter object length The only way I know is to use a for-loop to exhaust the index and then combine the cases back together. But will there be a simpler way to do so? Thank you very much for the help! - John ______________________________________________ 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.
David Winsemius
2009-Jun-03 22:55 UTC
[R] Select cases of data.frame based on a long index
On Jun 3, 2009, at 6:21 PM, tsunhin wong wrote:> Dear R users, > > I know how to select a subset of a data.frame or a matrix by setting > some criteria, but this time, I have a long index of cases not really > related to 1 or 2 criteria. > And I cannot find a simple way to select cases from another data.frame > based on this index (sublst in this example). >> sublst<-unique(g[comp.lst[[2]],]$Subject) >> sublst > [1] 19 20 21 22 23 24 25 26 27 28 29 31 32 33 34 35 36 37 38 39 41 42 > 43 44 45 46 47 48 49 > [30] 50 51 52 53 54 55 56 57 58 59 60 62 63 64 >> foo.lst <- subset(gTrialList,(TrialList==1 & Subject==sublst))Difficult because no reproducible code with data is offered. Have you considered substituting %in% for the "==" operator in subset?>> > Error: longer object length is not a multiple of shorter object > lengthSubject would be unlikely to be "==" to that vector.> > >> foo.lst <- subset(gTrialList,(TrialList==1)) >> foo.lst[foo.lst$Subject==sublst]Again, perhaps %in% might do the trick (if you also added a missing comma.) > s <- data.frame(cnt=1:10, LT=sample(LETTERS, 10) ) > s cnt LT 1 1 U 2 2 G 3 3 F 4 4 P 5 5 H 6 6 M 7 7 Q 8 8 N 9 9 T 10 10 B > > s[s$LT %in% c("F","N"), ] cnt LT 3 3 F 8 8 N>> > Error: > In foo.lst$Subject == sublst : > longer object length is not a multiple of shorter object length > > The only way I know is to use a for-loop to exhaust the index and then > combine the cases back together. But will there be a simpler way to do > so? > > Thank you very much for the help! > > - John > > ______________________________________________ > 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.David Winsemius, MD Heritage Laboratories West Hartford, CT