I would like to assign the i-th value of a vector to position [1,1]
of the i-th element a list of data.frames.
For example, I'd like to modify 'list1' as follows:
# list of data.frames
list1 <- list(data.frame(v1=c("a","x"),v2=c("x","x")), data.frame(v1=c("b","x"),v2=c("x","x")))
print(list1)
[[1]]
v1 v2
1 a x
2 x x
[[2]]
v1 v2
1 b x
2 x x
# vector of new values
new_elements <- c("c", "d")
# desired output
[[1]]
v1 v2
1 c x
2 x x
[[2]]
v1 v2
1 d x
2 x x
I have tried an approach similar to this post, without success. My attempt gives a list of the new elements only.
assignNew <- function(x, new, i) {x[[i]][x[[i]][[1, 1]]] <- new[i]}
lapply(seq_along(list1), assignNew, x = list1, new = new_elements)