[R] From Strings to Variable name. When get does not work

Prof Brian Ripley ripley at stats.ox.ac.uk
Wed Mar 12 09:02:44 CET 2014


On 12/03/2014 07:42, Alaios wrote:
> Hi all,
> I would like to turn some long strings like MyString$Myfield$MySubfield into variables but it looks like that the get does not like lists

Those are expressions not names, so you need to parse them.

 > eval(parse(text = "test$a"))
[1] 2

>
>
> so for example:
>
>   test<-list(a=2)
>
> test
>> $a
> [1] 2
>
>
> get("test")
>> $a
> [1] 2
>
>
> get("test$a")
>> Fehler in get("test$a") : Objekt 'test$a' nicht gefunden
>
> lapply(test,function(x) return (x$a))
>> Fehler in x$a : $ operator is invalid for atomic vectors
>
> What should I do to read those lists I have as strings?
>
> I would like to thank you in advance for your reply
>
> regards
> A
>
> 	[[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.
>


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