[R] subset select="variable with a list of names"

peter dalgaard pdalgd at gmail.com
Thu Feb 9 22:49:14 CET 2012


On Feb 9, 2012, at 18:17 , Nordlund, Dan (DSHS/RDA) wrote:

>> -----Original Message-----
>> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
>> project.org] On Behalf Of Francisco
>> Sent: Thursday, February 09, 2012 4:52 AM
>> To: r-help at r-project.org
>> Subject: [R] subset select="variable with a list of names"
>> 
>> Hello,
>> I would like to make a function which extracts a subset, from a
>> dataset,
>> with only the columns that I want (specifying their names).
>> 
>> For example, having this matrix:
>>> mydata<-matrix(c(22,1,3,2001,24,5,7,2002,26,7,8,2002,28,5,7,2003),
>> byrow=TRUE, ncol=4, dimnames=list(c(1,2,3,4),
>> c("age","day","month","year")))
>> 
>>> mydata
>> 
>>   age day month year
>> 1  22   1     3 2001
>> 2  24   5     7 2002
>> 3  26   7     8 2002
>> 4  28   5     7 2003
>> 
>> 
>> I would like to create a function like:
>> x<-function(names) {subset(mydata, select=names) }
>> 
>> So I can choose every time which columns select, i.e. when I call:
>> x("age,day")
>> 
>> it would returns:
>>   age day
>> 1  22   1
>> 2  24   5
>> 3  26   7
>> 4  28   5
>> 
>> Obviously it is not working, and I don't know how to do to fix it. Do
>> you have any suggestion?
>> 
>> Thank you very much
> 
> Given your function definition, the function call needs to be
> 
> x(c("age","day"))
> 
> Whether it is good form to write a function like this, I will leave to others to comment. 

Well, I'll bite. Using subset() inside functions is usually asking for trouble. 

It's much easier just to use the basic indexing features:

mydata[ , c("age","day"), drop=FALSE]

You can encapsulate that in a function, if you like (not that it really buys you much)

x <- function(names) mydata[, names, drop=FALSE]

(you may or may not want the drop=FALSE, see help(Extract))

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list