Re: [R] Using a string as a variable name

From: Duncan Murdoch <murdoch_at_stats.uwo.ca>
Date: Fri 20 Oct 2006 - 23:32:40 GMT

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



R-help@stat.math.ethz.ch 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. Received on Sat Oct 21 10:19:07 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 21 Oct 2006 - 00:30:11 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.