Search code examples
rlistassign

R assign along a vector


I have an ini-file, read as a list by R (in the example l). Now I want to add further sub-lists along a vector (m) and assign always the same constant to them. My attempt so far:

l <- list("A")
m <- letters[1:5]
n <- 5

for (i in 1:5){
  assign(paste0("l$A$",m[i]), n)
}
# which does not work   


# example of the desired outcome:
> l$A$e
[1] 5

I don't think that I have fully understood how lists work yet...


Solution

  • Try

    L[["A"]][m] <- n
    
    L$A$e
    # [1] 5
    

    Data:

    L <- list(A = list())
    m <- letters[1:5]
    n <- 5