[R] simple question

Thomas Lumley tlumley at u.washington.edu
Tue May 1 19:23:10 CEST 2001


On Tue, 1 May 2001, Martin Henry H. Stevens wrote:

> How does one turn a character string back into an object?
>
> I would like to take character strings and use them as objects, a process
> that I think is the reverse of deparse( substitute(X)).
> E.g., I would like to get the variable names from the fourth object listed
> with objects(),
> something like
>
> names(objects()[4]), but of course that doesn't work.
>

Amazingly, the opposite of deparse() is parse(), as suggested by
help(deparse)

> parse(text="trees")[[1]]
trees

So you can get the object with eval() and do eg
> data(trees)
> names(eval(parse(text="trees")[[1]]))
[1] "Girth"  "Height" "Volume"

or plug the parsed name into an expression like
> expr<-substitute(names(obj),list(obj=parse(text="trees")[[1]]))
> expr
names(trees)
> eval(expr)
[1] "Girth"  "Height" "Volume"


	-thomas

Thomas Lumley			Asst. Professor, Biostatistics
tlumley at u.washington.edu	University of Washington, Seattle



-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list