[R] Converting Character Strings into Object Names

Thomas Lumley tlumley at u.washington.edu
Fri May 24 22:15:20 CEST 2002


On Fri, 24 May 2002 Brian.J.GREGOR at odot.state.or.us wrote:

> Is there a function to convert character strings to object references.  For
> example, I want to name sets of objects based on scenarios and the types of
> data being stored.  So the scenarios might be something like c("a", "b",
> "c"), the types of data might have names like c("pass.trp", "trk.trp",
> "cmute.trp"), and I want to assign data to the objects a.pass.trp,
> b.pass.trp, c.pass.trp, a.trk.trp, etc.  I'd like to be able to paste the
> names together and then use them as an object references.

Yes.

The get() and assign() functions allow you to reference objects by
character strings, and as.name() will convert a string into a reference
(you would then probably need to use substitute() to create an expression
and eval()) to evaluate it.

eg

    assign("a",get("b"))
does
    a<-b


Often there is a better way: the objects can be stored in a list, making
it possible to use character strings directly to reference them

If  obj<-list(a=1,b=2)

then obj[["a"]] is a reference to the "a" element and you can do eg

obj[["a"]]<-otherobj[["b"]]


	-thomas

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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