I am struggling to populate a pre-defined 3-level nested list.
lst <- lapply(list("A1"=NULL, "B1"=NULL),
function(x){
lapply(list("Level2"=NULL),
function(y){
list("Level3"=NULL)})})
str(lst)
List of 2
$ A1:List of 1
..$ Level2:List of 1
.. ..$ Level3: NULL
$ B1:List of 1
..$ Level2:List of 1
.. ..$ Level3: NULL
Now, let's say we would like to replace every 3rd level by concatenating the names of the parent levels in order to get:
str(lst)
List of 2
$ A1:List of 1
..$ Level2:List of 1
.. ..$ Level3: "A1///Level2"
$ B1:List of 1
..$ Level2:List of 1
.. ..$ Level3: "B1///Level2"
Doing like follows drops the 2nd level names and disorganise the initial list structure:
lst[] <- lapply(names(lst),
function(x){
lapply(names(lst[[x]]),
function(y){
lst[[x]][[y]][["Level3"]] <- paste(x, y, sep="///")
}
)
}
)
str(lst)
List of 2
$ A1:List of 1
..$ : chr "A1///Level2"
$ B1:List of 1
..$ : chr "B1///Level2"
I am pretty sure it is a basic task, but I don't see why it does not work.