[R] assigning global columns selection for all subset functions in script

David Winsemius dwinsemius at comcast.net
Tue Jun 11 23:21:55 CEST 2013


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



More information about the R-help mailing list