[R] Extracting the name of an object into a character string and vice versa
David Winsemius
dwinsemius at comcast.net
Sat Dec 13 23:50:17 CET 2008
On Dec 12, 2008, at 8:41 AM, Gabor Grothendieck wrote:
> Its a FAQ:
>
> http://cran.r-project.org/doc/FAQ/R-FAQ.html#How-can-I-turn-a-string-into-a-variable_003f
>
> On Fri, Dec 12, 2008 at 8:30 AM, Philip Whittall
> <Philip.whittall at detica.com> wrote:
>>
>> I am still struggling to map a character string to an object name and
>> vice versa in R.
>> I thought the as.name() function might work, but observe the
>> following
>> behaviour ...
>>
>>> attach(warpbreaks)
>>> levels(tension)
>> [1] "L" "M" "H"
>>> levels(as.name("tension"))
>> NULL
>>> objectname<-as.name("tension")
>>> objectname
>> tension
>>> levels(objectname)
>> NULL
>>
>> So even though it sets up a symbol, this symbol isn't recognised as
>> an
>> object name by functions such as levels().
>>
>> I need 2 functions, call them A and B such that A("tension") yields
>> the
>> object name tension which is recognised by functions and
>> B(tension) yields the character result "tension".
I see that your first request was satisfied. Your second one, that for
a method to get as a character object the name of an object did not
seem to get addressed. If tension is an R object (or a function call),
try:
---a vector
> tension <- vector()
> as.character(bquote(tension))
[1] "tension"
--- a function with no arguments
> as.character(bquote(ls()))
[1] "ls"
--- a function ( in this case the Extract or indexing function) with
arguments
> as.character(bquote(fit$terms))
[1] "$" "fit" "terms"
--
David Winsemius
>>
>>
>> Any suggestions would be greatly appreciated,
>>
>> Philip
>>
>>
>>
>>
>>
>>
>>
>>
>>
>> This message should be regarded as confidential. If you have
>> received this email in error please notify the sender and destroy
>> it immediately.
>> Statements of intent shall only become binding when confirmed in
>> hard copy by an authorised signatory. The contents of this email
>> may relate to dealings with other companies within the Detica Group
>> plc group of companies.
>>
>> Detica Limited is registered in England under No: 1337451.
>>
>> Registered offices: Surrey Research Park, Guildford, Surrey, GU2
>> 7YP, England.
>>
>>
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list