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

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


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



More information about the R-help mailing list