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

Jinsong Zhao jszhao at yeah.net
Sun Apr 30 17:20:17 CEST 2017


On 2017/4/30 23:17, Jinsong Zhao 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
      # wrong code in previous message, the correct on should be:
      assign(paste("list_", i, "[[1]]", sep = ""), 5) # does not work...
> }
>
> How to do? Is there any alternatives? Many thanks!
>
> Best,
> Jinsong



More information about the R-help mailing list