[R] how to assign a value to a specific position of a list

Bert Gunter bgunter.4567 at gmail.com
Sun Apr 30 19:14:23 CEST 2017


... and moreover, note that the assignment can even be shortened to:


> for ( i in 1:10 )  l[[c(i,1)]] <- 5

?"[["  contains details, but the relevant point is:

"[[ can be applied recursively to lists, so that if the single index i
is a vector of length p, alist[[i]] is equivalent to
alist[[i1]]...[[ip]] providing all but the final indexing results in a
list."

For a less terse version, see any good online R tutorial. Lists are
extremely useful in R, and indexing is fundamental. If you haven't
spent the time to learn about these constructs, you should now before
posting further. You'll save yourself a  lot of grief and perhaps even
some embarassment.


Cheers,
Bert


Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sun, Apr 30, 2017 at 8:48 AM, peter dalgaard <pdalgd at gmail.com> wrote:
> assign(paste("list_", i, "[[1]]", sep = ""), 5) creates a new variable with a funny name.
>
> You'd have to parse() and eval() to make that work, something like
>
> eval(parse(text=paste("list_",i,"[[1]]<-",5, sep="")))
>
> However,
> -------
>> fortunes::fortune("parse")
>
> If the answer is parse() you should usually rethink the question.
>    -- Thomas Lumley
>       R-help (February 2005)
> -------
>
> It is much easier to handle this using a data structure containing a list of lists:
>
> l <- rep(list(list()), 10)
> for ( i in 1:10 )
>    l[[i]][[1]] <- 5
>
>> On 30 Apr 2017, at 17:17 , Jinsong Zhao <jszhao at yeah.net> wrote:
>>
>> Hi there,
>>
>> I have a problem with assign(). Here is the demo code:
>>
>> for (i in 1:10) {
>>   # create a list with variable name as list_1, list_2, ..., etc.
>>   assign(paste("list_", i, sep = ""), list())
>>   # I hope to assign 5 to list_?[[1]], but I don't know how to code it.
>>   # list_1[[1]] <- 5 # works, however
>>   assign(paste("list_", i, "[[1]]", sep = "", 5) # does not work
>> }
>>
>> How to do? Is there any alternatives? Many thanks!
>>
>> Best,
>> Jinsong
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>
> --
> Peter Dalgaard, Professor,
> Center for Statistics, Copenhagen Business School
> Solbjerg Plads 3, 2000 Frederiksberg, Denmark
> Phone: (+45)38153501
> Office: A 4.23
> Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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