[R] converting a character string to a R object

Prof Brian Ripley ripley at stats.ox.ac.uk
Thu Jan 8 14:31:40 CET 2004


Please do use a meaningful subject line.

You are looking for get(): get("a") returns the R object named "a" (if one 
is in scope).

Note that if you use R-devel (the development version of R, see the FAQ) 
you _can_ use underscore in object names.

On Thu, 8 Jan 2004, Sixten Borg wrote:

> I have trouble converting a character string to a R object. Let me
> describe this by an example;
> 
> > dim(a)
> [1] 270  14
> > dim("a")
> NULL
> 
> > names(a)
>  [1] "Var1"  "Var2"  "Var3"  "Var4"  "Var5"  "Var6"  "Var7"  "Var8"  "Var9" 
> [10] "Var10" "Var11" "Var12" "Var13" "Var14"
> > names("a")
> NULL
> 
> I realise that the character string lacks both a dimension and any
> column names; my question is how to make R understand that I look for
> the object a when I write "a".
> 
> Like a type cast in C;      (R data.frame) "a"      for those familiar with C.

Rather, more like following a pointer in C.  A cast would be
as.data.frame("a")  or as("a", "data.frame"), which is not what you want.

> The underlying reason for this is that I am writing a script that
> imports several datasets. The file names of the datasets contain the '_'
> character which forces me to construct a valid dataset name for each
> file. Although I can do this by hand, I would like to know if there is
> any solution to my first approach.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list