[R] Using a string as a variable name

Duncan Murdoch murdoch at stats.uwo.ca
Sat Oct 21 01:32:40 CEST 2006


On 10/20/2006 7:28 PM, Jason Horn wrote:
> Is it possible to use a string as a variable name?  For example:
> 
> foo<="var1"
> frame$foo   # frame is a data frame with with a column titled "var1"
> 
> This does not work, unfortunately.  Am I just missing the correct  
> syntax to make this work?

Yes, you want

foo <- "var1"
frame[ ,foo]

(i.e. return the foo column from frame, indexing frame as though it was 
a matrix).

Duncan Murdoch



More information about the R-help mailing list