I have a question about how to filter the data frame: Suppose my data frame has variables like gender, age,... How to get a subset of the data frame, with only female (or male) and/or age > 50...? What is the typical syntax? I tried several condition expressions, but none of them worked... Thanks a lot! -- View this message in context: http://www.nabble.com/How-to-filter-a-data-frame--tp18587502p18587502.html Sent from the R help mailing list archive at Nabble.com.
?subset Is this what you want? subset(dataset, gender="M" | age < 50) --- On Tue, 7/22/08, rlearner309 <unixunix99 at gmail.com> wrote:> From: rlearner309 <unixunix99 at gmail.com> > Subject: [R] How to filter a data frame? > To: r-help at r-project.org > Received: Tuesday, July 22, 2008, 9:47 AM > I have a question about how to filter the data frame: > Suppose my data frame has variables like gender, age,... > How to get a subset > of the data frame, with only female (or male) and/or age > > 50...? What is > the typical syntax? I tried several condition expressions, > but none of them > worked... > > Thanks a lot! > -- > View this message in context: > http://www.nabble.com/How-to-filter-a-data-frame--tp18587502p18587502.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.__________________________________________________________________ [[elided Yahoo spam]]
on 07/22/2008 08:47 AM rlearner309 wrote:> I have a question about how to filter the data frame: > Suppose my data frame has variables like gender, age,... How to get a subset > of the data frame, with only female (or male) and/or age > 50...? What is > the typical syntax? I tried several condition expressions, but none of them > worked... > > Thanks a lot!See ?subset and ?Logic For example: DF.new <- subset(DF, (gender == "female") & (age > 50)) HTH, Marc Schwartz
May be this help you; Just intall.packages("sqldf", dependencies=T) require(sqldf) my_df<-data.frame(cbind(gender=sample(c("male","female"),50,replace=T), age=round(rnorm(50, mean=30, sd=5),0))) my_df_subset_male<-sqldf("select * from my_df where gender=='male'") my_df_subset_male my_df_subset_male_greater35<-sqldf("select * from my_df where gender=='male' and age>35") my_df_subset_male_greater35 Good luck miltinho astronauta brazil On 7/22/08, rlearner309 <unixunix99@gmail.com> wrote:> > > I have a question about how to filter the data frame: > Suppose my data frame has variables like gender, age,... How to get a > subset > of the data frame, with only female (or male) and/or age > 50...? What is > the typical syntax? I tried several condition expressions, but none of > them > worked... > > Thanks a lot! > -- > View this message in context: > http://www.nabble.com/How-to-filter-a-data-frame--tp18587502p18587502.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Hi, rlearner309 wrote:> I have a question about how to filter the data frame: > Suppose my data frame has variables like gender, age,... How to get a subset > of the data frame, with only female (or male) and/or age > 50...? What is > the typical syntax? I tried several condition expressions, but none of them > worked... > > Thanks a lot!I guess the other people answered the question already. But may I ask if your background is in SPSS? I just had this impression talking about 'filter' and the unique way of using 'syntax'. Best, Roland P.S. The first statistical software I used was SPSS and I would have asked in exactly the same way when I started using R. :-)
Thank you all!! :-) rlearner309 wrote:> > I have a question about how to filter the data frame: > Suppose my data frame has variables like gender, age,... How to get a > subset of the data frame, with only female (or male) and/or age > 50...? > What is the typical syntax? I tried several condition expressions, but > none of them worked... > > Thanks a lot! >-- View this message in context: http://www.nabble.com/How-to-filter-a-data-frame--tp18587502p18598469.html Sent from the R help mailing list archive at Nabble.com.