Dear R users, I have a simple question on variable manipulation. Imagine I have an object "OBJ" that has "toto" as one of its variables. I would like to understand why if I do > varname <- "toto" >OBJ$varname returns no results whereas > OBJ[varname] returns the column entitled "toto" Thanks for your help. St?phane.
see ?"$"
'x$name' is equivalent to 'x[["name"]]'
so you need use :
eval(parse(text = paste("OBJ$", varname)))
-------------------------------------------------------------------
Jacques VESLOT
CNRS UMR 8090
I.B.L (2?me ?tage)
1 rue du Professeur Calmette
B.P. 245
59019 Lille Cedex
Tel : 33 (0)3.20.87.10.44
Fax : 33 (0)3.20.87.10.31
http://www-good.ibl.fr
-------------------------------------------------------------------
St?phane Cruveiller a ?crit :> Dear R users,
>
> I have a simple question on variable manipulation.
> Imagine I have an object "OBJ" that has "toto" as one
of its variables.
> I would like to understand why if I do
>
> > varname <- "toto"
>
> >OBJ$varname returns no results
>
> whereas
>
> > OBJ[varname] returns the column entitled
> "toto"
>
>
> Thanks for your help.
>
> St?phane.
>
> ______________________________________________
> R-help at 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
>
St?phane Cruveiller wrote:> Dear R users, > > I have a simple question on variable manipulation. > Imagine I have an object "OBJ" that has "toto" as one of its variables. > I would like to understand why if I do > > > varname <- "toto" > > >OBJ$varname returns no results > > whereas > > > OBJ[varname] returns the column entitled > "toto" > > > Thanks for your help. > > St?phane. >because if the value of `varname' is substituted in the expressions, in the first case that yields OBJ$"toto" and in the second OBJ["toto"] the latter is valid, the former is not (you'd need `OBJ$toto' there), read ` ?"$" ': "...Both '[[' and '$' select a single element of the list. The main difference is that '$' does not allow computed indices, whereas '[[' does. 'x$name' is equivalent to 'x[["name"]]'..." not, too, the difference between `[' (sublist) and `[[' (single element extraction) joerg