I have a nested list with two or more elements at the top level and want to modify named elements of the sub-lists, e.g. by appending values from a named vector.
Let's assume L is a list of two elements and Nis a vector with the same number of elements:
L <- list(
foo = list(
x1=1,
x2=c(a=1, b=2)
),
bar = list(
x1=2,
x2=c(e=2, f=3)
)
)
N <- c(n=11, n=12)
Then I want to get a result like this:
list(
foo = list(
x1=1,
x2=c(a=1, b=2, n=11)
),
bar = list(
x1=2,
x2=c(e=2, f=3, n=12)
)
)
It is easy to read the elements of the sub-lists:
lapply(L, "[[", "x2")
So I wonder if there is a similarly compact way to change elements.