[R] Using the value of a variable as a variable

(Ted Harding) Ted.Harding at manchester.ac.uk
Wed Jan 30 22:15:11 CET 2008


On 30-Jan-08 19:47:55, Ramon Hidalgo wrote:
> Hello,
> 
> How can I make the following expressions are equivalent
> datos$Col1 and datos$var when I define var <- "Col1"?
> 
> I am trying to get the same result with
>> datos$Col1
>  [1] 0 1 1 0 1 0 1 1 0
> 
> And
>> datos$var
> NULL

It will work if you use [], though I'm not sure that
this is the best or orthodox way to do it:

  datos<-data.frame(Col1=c(0,1,1,0,1,0,1,1,0),Col2=c(1,2,2,1,2,1,2,2,1))
  var<-"Col1"
  datos[var]
##  Col1
##1    0
##2    1
##3    1
##4    0
##5    1
##6    0
##7    1
##8    1
##9    0

Hoping this helps,
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 30-Jan-08                                       Time: 21:15:07
------------------------------ XFMail ------------------------------



More information about the R-help mailing list