Re: [R] Access and assign list sub-elements using a string suchas "l$a$b"

From: Joerg van den Hoff <j.van_den_hoff_at_fz-rossendorf.de>
Date: Fri 16 Jun 2006 - 02:55:46 EST

Petr Pikal wrote:

> Hi
> 
> yes you are correct, I remembered there is something with eval from 
> older posts but did not find a connection to parse from eval help 
> page. Shouldn't there be a link? Or even an example?

would be a good thing to do (there only is a link from parse to eval). after all eval(parse(text = some_string)) is what many users (which might come from matlab/octave where this _would_ suffice) want when they try out eval(some_string)). and a standard eval(parse(text=...) example on the eval manpage (where most people probably would look, would be very good.

> 
> Best regards
> Petr
> 
> 
> On 15 Jun 2006 at 17:21, Dimitris Rizopoulos wrote:
> 
> From:           	"Dimitris Rizopoulos" <dimitris.rizopoulos@med.kuleuven.be>
> To:             	"Petr Pikal" <petr.pikal@precheza.cz>, <gsxej2@cam.ac.uk>
> Copies to:      	<r-help@stat.math.ethz.ch>
> Subject:        	Re: [R] Access and assign list sub-elements using a string suchas	"l$a$b"
> Date sent:      	Thu, 15 Jun 2006 17:21:26 +0200
> 

>> ----- Original Message -----
>> From: "Petr Pikal" <petr.pikal@precheza.cz>
>> To: "Gregory Jefferis" <gsxej2@cam.ac.uk>
>> Cc: <r-help@stat.math.ethz.ch>
>> Sent: Thursday, June 15, 2006 4:56 PM
>> Subject: Re: [R] Access and assign list sub-elements using a string
>> suchas "l$a$b"
>>
>>
>>> Hi
>>> very, very close
>>>
>>>
>>> On 15 Jun 2006 at 13:27, Gregory Jefferis wrote:
>>>
>>> Date sent: Thu, 15 Jun 2006 13:27:05 +0100
>>> From: Gregory Jefferis <gsxej2@cam.ac.uk>
>>> To: "r-help-request@stat.math.ethz.ch"
>>> <r-help-request@stat.math.ethz.ch>
>>> Forwarded to: <r-help@stat.math.ethz.ch>
>>> Forwarded by: Gregory Jefferis <gsxej2@cam.ac.uk>
>>> Date forwarded: Thu, 15 Jun 2006 14:54:13 +0100
>>> Subject: [R] Access and assign list sub-elements using a
>>> string such as "l$a$b"
>>>
>>>> If I have a list I can set a sub-element as follows on the command
>>>> line:
>>>>
>>>> people=list()
>>>> people$tom$hair="brown"
>>>> people
>>>>
>>>> But what if I have a string containing the name of the sub-element
>>>> that I want to access?
>>>>
>>>> subel= "people$tom$hair"
>>>>
>>>> get(subel) # returns error
>>>> assign(subel,"red") # silent but doesn't change list
>>>> people
>>> See what happens when
>>>
>>> people<-assign(subel, "red")
>> but I think this is not what Greg wanted; the above just assigns "red"
>> to object 'people' (i.e., check `str(assign(subel, "red"))'). If I
>> understood correctly, the following could be of help:
>>
>> people <- list()
>> people$tom$hair <- "brown"
>> people
>> #################
>> subel <- "people$tom$hair"
>> eval(parse(text = subel))
>> eval(parse(text = paste(subel, "<- 'red'")))
>> people
>>
>>
>> Best,
>> Dimitris
>>
>>
>> ----
>> Dimitris Rizopoulos
>> Ph.D. Student
>> Biostatistical Centre
>> School of Public Health
>> Catholic University of Leuven
>>
>> Address: Kapucijnenvoer 35, Leuven, Belgium
>> Tel: +32/(0)16/336899
>> Fax: +32/(0)16/337015
>> Web: http://med.kuleuven.be/biostat/
>> http://www.student.kuleuven.be/~m0390867/dimitris.htm
>>
>>
>>> HTH
>>> Petr
>>>
>>>
>>>> The attempts above using assign/get won't do what I am trying to do
>>>> [nor according to the help should they]. I would be very grateful
>>>> for any suggestions. Many thanks,
>>>>
>>>> Greg.
>>>>
>>>> --
>>>> Gregory Jefferis, PhD and:
>>>> Research Fellow
>>>> Department of Zoology St John's
>>>> College University of Cambridge Cambridge Downing Street
>>>> CB2 1TP Cambridge, CB2 3EJ United Kingdom
>>>>
>>>> Lab Tel: +44 (0)1223 336683 Office: +44 (0)1223
>>>> 339899 Lab Fax: +44 (0)1223 336676
>>>>
>>>> http://www.zoo.cam.ac.uk/zoostaff/jefferis.html
>>>> gsxej2@cam.ac.uk
>>>>
>>>> ______________________________________________
>>>> R-help@stat.math.ethz.ch mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide!
>>>> http://www.R-project.org/posting-guide.html
>>> Petr Pikal
>>> petr.pikal@precheza.cz
>>>
>>> ______________________________________________
>>> R-help@stat.math.ethz.ch mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide!
>>> http://www.R-project.org/posting-guide.html
>>>
>>
>> Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
>>
> 
> Petr Pikal
> petr.pikal@precheza.cz
> 
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Fri Jun 16 02:59:10 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Fri 16 Jun 2006 - 04:12:58 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.