Francisco J. Bido
2003-Aug-29 06:09 UTC
[R] Creating a new table from a set of constraints
Hi Everyone, Here's a silly newbie question. How do I remove unwanted rows from an R table? Say that I read my data as: X <- read.table("mydata.txt") and say that there are columns for age and gender. Call these X[5] and X[10], respectively. Here, X[5] is a column of positive integers and X[10] is binary valued i.e., zero (for male) and one (for female) Now, say that I want to form a new table called Y which has the following constraints: 1. Only females that are between 18 and 40 years old. 2. Only males that are between 20 and 30 years old I can do this using a typical procedural approach (no different than C programmer would) but it seems to me that R has many shortcuts and so I thought I ask first before heading on an inefficient path. What's a good way of doing this, my data set is very large? Thanks, -Francisco
Hi Francisco, what I would do : names(X)[c(5,10)]<-c("Age","Gender") Xnew1<-X[X$Gender==1 & X$Age>=18 & X$Age <=40,] Xnew2<-X[X$Gender==0 & X$Age>=20 & X$Age <=30,] Xnew<-rbind(Xnew1,Xnew2) But there must be something more elegant, Good luck, Arnaud -----Original Message----- From: Francisco J. Bido [mailto:bido at mac.com] Sent: Friday, 29 August 2003 4:10 PM To: r-help at stat.math.ethz.ch Subject: [R] Creating a new table from a set of constraints Hi Everyone, Here's a silly newbie question. How do I remove unwanted rows from an R table? Say that I read my data as: X <- read.table("mydata.txt") and say that there are columns for age and gender. Call these X[5] and X[10], respectively. Here, X[5] is a column of positive integers and X[10] is binary valued i.e., zero (for male) and one (for female) Now, say that I want to form a new table called Y which has the following constraints: 1. Only females that are between 18 and 40 years old. 2. Only males that are between 20 and 30 years old I can do this using a typical procedural approach (no different than C programmer would) but it seems to me that R has many shortcuts and so I thought I ask first before heading on an inefficient path. What's a good way of doing this, my data set is very large? Thanks, -Francisco ______________________________________________ R-help at stat.math.ethz.ch mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help ********************************DISCLAIMER******************...{{dropped}}
Francisco J. Bido
2003-Aug-29 06:46 UTC
[R] Creating a new table from a set of constraints
Thanks everyone! I now see how to handle the situation. This has to be the most responsive mailing list ever... Best, -Francisco On Friday, August 29, 2003, at 01:39 AM, Andrew Hayen wrote:> Also see this page: http://www.ats.ucla.edu/stat/SPLUS/faq/subset_R.htm > > A > > -----Original Message----- > From: Francisco J. Bido [mailto:bido at mac.com] > Sent: Friday, 29 August 2003 4:10 PM > To: r-help at stat.math.ethz.ch > Subject: [R] Creating a new table from a set of constraints > > > Hi Everyone, > > Here's a silly newbie question. How do I remove unwanted rows from an > R table? Say that I read my data as: > > X <- read.table("mydata.txt") > > and say that there are columns for age and gender. Call these X[5] and > X[10], respectively. > Here, X[5] is a column of positive integers and X[10] is binary valued > i.e., zero (for male) and one (for female) > > Now, say that I want to form a new table called Y which has the > following constraints: > > 1. Only females that are between 18 and 40 years old. > 2. Only males that are between 20 and 30 years old > > I can do this using a typical procedural approach (no different than C > programmer would) but it seems > to me that R has many shortcuts and so I thought I ask first before > heading on an inefficient path. What's > a good way of doing this, my data set is very large? > > Thanks, > -Francisco > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
I would use the subset() function. Assuming the data frame has variable names "Gender" and "Age", you could do: Y1 <- subset(X, Gender == 1 & Age >= 18 & Age <= 40) Y2 <- subset(X, Gender == 0 & Age >= 20 & Age <= 30) -roger Francisco J. Bido wrote:> Hi Everyone, > > Here's a silly newbie question. How do I remove unwanted rows from an > R table? Say that I read my data as: > > X <- read.table("mydata.txt") > > and say that there are columns for age and gender. Call these X[5] > and X[10], respectively. > Here, X[5] is a column of positive integers and X[10] is binary > valued i.e., zero (for male) and one (for female) > > Now, say that I want to form a new table called Y which has the > following constraints: > > 1. Only females that are between 18 and 40 years old. > 2. Only males that are between 20 and 30 years old > > I can do this using a typical procedural approach (no different than C > programmer would) but it seems > to me that R has many shortcuts and so I thought I ask first before > heading on an inefficient path. What's > a good way of doing this, my data set is very large? > > Thanks, > -Francisco > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >