Dear R experts, My problem is as follows: Suppose I have a data frame d comprising two variable a<-c(1:10) & b<-c(11:20). I now want to select a subgroup according the values of b. I know if I just want to select, say, b=17, I can use f<-d[d$b==17] and R will give me> fa b 7 7 17 However, if now I want to select a subgroup according to b==e<-c(13,15,17), then the same syntx doesn't work. What is the correct way to do it? My data have more than one million subjects, and I want to select part of them according to their id numbers. Your help will be highly appreciated. Best regards, Yu-Kang
On Wed, 2005-05-04 at 11:14 +0000, Tu Yu-Kang wrote:> Dear R experts, > > My problem is as follows: > > Suppose I have a data frame d comprising two variable a<-c(1:10) & > b<-c(11:20). > > I now want to select a subgroup according the values of b. > > I know if I just want to select, say, b=17, I can use f<-d[d$b==17] and R > will give me > > > f > a b > 7 7 17 > > However, if now I want to select a subgroup according to b==e<-c(13,15,17), > then the same syntx doesn't work. > > What is the correct way to do it? My data have more than one million > subjects, and I want to select part of them according to their id numbers. > > Your help will be highly appreciated. > > Best regards, > > Yu-KangYou would want to use something like the following:> df <- data.frame(a = 1:10, b = 11:20)> dfa b 1 1 11 2 2 12 3 3 13 4 4 14 5 5 15 6 6 16 7 7 17 8 8 18 9 9 19 10 10 20> df[df$b %in% c(13, 15, 17), ]a b 3 3 13 5 5 15 7 7 17 See ?"%in%" for more information. Also, see ?subset for more flexibility in using complex boolean expressions for subsetting. HTH, Marc Schwartz
try this: d <- data.frame(a=1:10, b=11:20) e <- c(13, 15, 17) ############## d. <- subset(d, b %in% e) d. I hope it helps. Best, Dimitris ---- Dimitris Rizopoulos Ph.D. Student Biostatistical Centre School of Public Health Catholic University of Leuven Address: Kapucijnenvoer 35, Leuven, Belgium Tel: +32/16/336899 Fax: +32/16/337015 Web: http://www.med.kuleuven.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Tu Yu-Kang" <yukangtu at hotmail.com> To: <R-help at stat.math.ethz.ch> Sent: Wednesday, May 04, 2005 1:14 PM Subject: [R] selections of data by one variable> Dear R experts, > > My problem is as follows: > > Suppose I have a data frame d comprising two variable a<-c(1:10) & > b<-c(11:20). > > I now want to select a subgroup according the values of b. > > I know if I just want to select, say, b=17, I can use f<-d[d$b==17] > and R will give me >> f > a b > 7 7 17 > > However, if now I want to select a subgroup according to > b==e<-c(13,15,17), then the same syntx doesn't work. > > What is the correct way to do it? My data have more than one > million subjects, and I want to select part of them according to > their id numbers. > > Your help will be highly appreciated. > > Best regards, > > Yu-Kang > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >
Tu Yu-Kang wrote:> Dear R experts, > > My problem is as follows: > > Suppose I have a data frame d comprising two variable a<-c(1:10) & > b<-c(11:20). > > I now want to select a subgroup according the values of b. > > I know if I just want to select, say, b=17, I can use f<-d[d$b==17] and > R will give me > >> f > > a b > 7 7 17 > > However, if now I want to select a subgroup according to > b==e<-c(13,15,17), then the same syntx doesn't work.Which language is this??? To summarize, all the code you specified is: a<-c(1:10) & b<-c(11:20) f<-d[d$b==17] b==e<-c(13,15,17) In R, each line for itself is syntacically completely incorrect (even if you say something would work, which is definitely not the case)! "PLEASE do read the posting guide!" I guess you want something like d <- data.frame(a = 1:10, b = 11:20) subset(d, b == 17) e <- c(13, 15, 17) subset(d, b %in% e) Uwe Ligges> What is the correct way to do it? My data have more than one million > subjects, and I want to select part of them according to their id numbers. > > Your help will be highly appreciated. > > Best regards, > > Yu-Kang > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html
test <- data.frame(cbind(1:10,11:20)) names(test) <- c("a", "b") test[test$b == 17,] test[test$b %in% c(13, 15, 17),] Tu Yu-Kang wrote:> Dear R experts, > > My problem is as follows: > > Suppose I have a data frame d comprising two variable a<-c(1:10) & > b<-c(11:20). > > I now want to select a subgroup according the values of b. > > I know if I just want to select, say, b=17, I can use f<-d[d$b==17] and > R will give me >> f > a b > 7 7 17 > > However, if now I want to select a subgroup according to > b==e<-c(13,15,17), then the same syntx doesn't work. > > What is the correct way to do it? My data have more than one million > subjects, and I want to select part of them according to their id numbers. > > Your help will be highly appreciated. > > Best regards, > > Yu-Kang > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html > >