[R] Selecting A List of Columns

peter dalgaard pdalgd at gmail.com
Fri May 17 12:07:35 CEST 2013


On May 17, 2013, at 12:02 , peter dalgaard wrote:

> 
> On May 17, 2013, at 08:51 , Sparks, John James wrote:
> 
>> Dear R Helpers,
>> 
>> I need help with a slightly unusual situation in which I am trying to
>> select some columns from a data frame.  I know how to use the subset
>> statement with column names as in:
> 
> Notice that subset() is a convenience function for command line use. The non-standard evaluation tricks in it tend to become inconveniences if you try to use subset() in a function (I can say that, I wrote the blasted thing...). Just use normal subseting functions instead and everything behaves much more predictably. If ImportantVars is a vector of column names, use
> 
> mtcars[ImportantVars] 
> 
> (or mtcars[,ImportantVars], which also works for matrices).
> 


Oups, Pascal has the right end of the stick. The above is correct, but the "If" is important: You need a character vector of names, and that's not what is in ImportantVars.

-- 
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