bcrombie
2013-Jun-11 16:18 UTC
[R] assigning global columns selection for all subset functions in script
How do I let R know that I always want to select the same columns in my subset functions (below), so that I don't have to keep copy/pasting the same selection? (thanks) devUni2 <- subset(devUni1, dind02 != 52, select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) devUni3 <- subset(devUni2, lfsr94 == 1 | lfsr94 == 2 , select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) devUni4 <- subset(devUni3, class94 < 6 , select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) devUni5 <- subset(devUni4, relref95 < 10 | relref95 ==13 | relref95 >=14, select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) -- View this message in context: http://r.789695.n4.nabble.com/assigning-global-columns-selection-for-all-subset-functions-in-script-tp4669252.html Sent from the R help mailing list archive at Nabble.com.
David Winsemius
2013-Jun-11 21:21 UTC
[R] assigning global columns selection for all subset functions in script
On Jun 11, 2013, at 9:18 AM, bcrombie wrote:> How do I let R know that I always want to select the same columns in my > subset functions (below), so that I don't have to keep copy/pasting the same > selection? (thanks) > devUni2 <- subset(devUni1, dind02 != 52, > select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year))Perhaps: devUni3 <- subset(devUni2, lfsr94 == 1 | lfsr94 == 2 , select=names(devUni2) ) Or just create a character vector: desired <- names(devUni2) devUni3 <- subset(devUni2, lfsr94 == 1 | lfsr94 == 2 , select=desired ) Because I am lazy I exprimented a bit to avoid adding all those quote marks: desired <- expression(paidhre, earnhre,earnwke,uhourse,hourslw,otc,ind02, dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year) as.character(as.list(desired)) [1] "paidhre" "earnhre" "earnwke" "uhourse" "hourslw" "otc" "ind02" [8] "dind02" "occ00" "docc00" "lfsr94" "class94" "relref95" "smsastat" [15] "state" "weight" "year"> devUni4 <- subset(devUni3, class94 < 6 , > select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) > devUni5 <- subset(devUni4, relref95 < 10 | relref95 ==13 | relref95 >=14, > select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) >-- David Winsemius Alameda, CA, USA
John Kane
2013-Jun-11 21:26 UTC
[R] assigning global columns selection for all subset functions in script
index the columns to select lets say you want to select a set of colmns 2,4,6,8 Try something like this. (not run) mycols <- c(2,4,6,8) select(mydata[ , mycols] , mdata$x == 3) John Kane Kingston ON Canada> -----Original Message----- > From: bcrombie at utk.edu > Sent: Tue, 11 Jun 2013 09:18:25 -0700 (PDT) > To: r-help at r-project.org > Subject: [R] assigning global columns selection for all subset functions > in script > > How do I let R know that I always want to select the same columns in my > subset functions (below), so that I don't have to keep copy/pasting the > same > selection? (thanks) > devUni2 <- subset(devUni1, dind02 != 52, > select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) > devUni3 <- subset(devUni2, lfsr94 == 1 | lfsr94 == 2 , > select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) > devUni4 <- subset(devUni3, class94 < 6 , > select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) > devUni5 <- subset(devUni4, relref95 < 10 | relref95 ==13 | relref95 > >=14, > select=c(paidhre,earnhre,earnwke,uhourse,hourslw,otc,ind02,dind02,occ00,docc00,lfsr94,class94,relref95,smsastat,state,weight,year)) > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/assigning-global-columns-selection-for-all-subset-functions-in-script-tp4669252.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.____________________________________________________________ FREE 3D EARTH SCREENSAVER - Watch the Earth right on your desktop!