[R] character string to name

Prof Brian Ripley ripley at stats.ox.ac.uk
Tue Jul 10 09:00:24 CEST 2007


On Mon, 9 Jul 2007, Thomas Lumley wrote:

> On Mon, 9 Jul 2007, Jim Lemon wrote:
>
>> Hi folks,
>>
>> I thought I recalled a request for turning a character string into an
>> object name as in:
>
> Yes. It's a FAQ.

There is an FAQ about turning character strings into objects (Q7.21), but 
this seems a bit different.  In the first example, he really does want a 
name.  Since $ does not evaluate its rhs, ways to do that are

yy <- "y"
x$[[yy]] <- 1:4
eval(substitute(x$yy <- 1:4, list(yy=yy)))

and of course parse(text=).

For the second, I think something like

e <- quote(data.frame(yy=1:4))
names(e)[2] <- yy
x <- eval(e)

is probably the simplest way.

>
> 	-thomas
>
>
>> x$as.name("y")<-1:4
>>
>> OR
>>
>> x<-data.frame(as.name("y")=1:4)
>>
>> However, as.name and a few other uninformed attempts didn't even come
>> close. A search of "character to name" produced no helpful functions.
>> This isn't a very urgent request, but if anyone knows some trick to
>> perform this transformation, I would like to hear about it. Thanks.
>>
>> Jim

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