[R] Map a string to an object

Marc Schwartz MSchwartz at MedAnalytics.com
Thu Apr 14 01:59:52 CEST 2005


On Wed, 2005-04-13 at 19:46 -0400, Fernando Saldanha wrote:
> Is there a way in R to get an object whose name is given by a string?
> 
> That is, like a function getObject(mystring) such that
> 
> getObject('astring') 
> 
> returns the object astring (assuming it exists)?
> 
> Thanks.

Yep. You are close.

See ?get

> x <- 1:10

> get("x")
 [1]  1  2  3  4  5  6  7  8  9 10


> get("ls")
function (name, pos = -1, envir = as.environment(pos), all.names =
FALSE,
    pattern)
{
    if (!missing(name)) {
        nameValue <- try(name)
...


HTH,

Marc Schwartz




More information about the R-help mailing list