[R] name of a name

baptiste auguie baptiste.auguie at googlemail.com
Thu Nov 19 10:39:23 CET 2009


Hi,

Try this,


d <- data.frame(a=1:4, b=3:6)

var <- "a"

mean(d[var])

## or, if you are not aware of
## fortune("parse")

xx <- paste("d$",var, sep="")
mean(eval(parse(text=xx)))

HTH,

baptiste




2009/11/19 William Simpson <william.a.simpson at gmail.com>:
> I have quite a complicated problem that's hard to describe.
>
> Suppose I have a dataframe d. I want to access the vector d$var, where
> var is one of the variables in d. Just take for granted that there's a
> good reason I want to do this as follows.
>
> var<-"DeOxyA"
> xx<-paste("d$",var, sep="")
> mean(xx)
> [1] NA
> Warning message:
> In mean.default(xx) : argument is not numeric or logical: returning NA
>
>> mean(d$DeOxyA)
> [1] 21.98904
>
> How can I convert xx so I can do mean(xx), for example? Currently xx
> is a character, not a variable.
> as.numeric doesn't do it. I want R to see
> mean(DeOxyA), not
> mean("DeOxyA")
>
> I'm stumped.
>
> Thanks very much for any help.
>
> Bill
>
> ______________________________________________
> R-help at r-project.org 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.
>




More information about the R-help mailing list