Reputation: 1480
I would like to append to z the below list, however one sublist at a time in specific locations within z. I tried to do this with the below expression however I can not manage to have this in proper order.
z <- list (c(3,2,1), c(4,6,5), c(7,8,9), c(3,2,1), c(4,6,5), c(7,8,9))
y <- list (c("A","B","C"), c("D","E","F"), c("h","i","j"))
fun <- function (x) append (z , y[[x]] , c(0,2,4))
lapply (seq(length( y )) , fun )
# Desired Output
x
[[1]]
[1] "A","B","C
[[2]]
[1] 3 2 1
[[3]]
[1] 4 6 5
[[4]]
[1] "D","E","F"
[[5]]
[1] 7 8 9
[[6]]
[1] 3 2 1
[[7]]
[1] "h","i","j"
[[8]]
[1] 4 6 5
[[9]]
[1] 7 8 9
Upvotes: 3
Views: 54
Reputation: 4472
Here is another option
c(y,z)[order(c(seq_along(y),rep(1:length(y), each = 2)))]
#[[1]]
#[1] "A" "B" "C"
#
#[[2]]
#[1] 3 2 1
#
#[[3]]
#[1] 4 6 5
#
#[[4]]
#[1] "D" "E" "F"
#
#[[5]]
#[1] 7 8 9
#
#[[6]]
#[1] 3 2 1
#
#[[7]]
#[1] "h" "i" "j"
#
#[[8]]
#[1] 4 6 5
#
#[[9]]
#[1] 7 8 9
Upvotes: 3
Reputation: 887213
We can try
v1 <- c(0,2,4)
v2 <- v1 + seq_along(v1)-1
for(i in seq_along(y)) z <- append(z,y[i], v2[i])
z
#[[1]]
#[1] "A" "B" "C"
#[[2]]
#[1] 3 2 1
#[[3]]
#[1] 4 6 5
#[[4]]
#[1] "D" "E" "F"
#[[5]]
#[1] 7 8 9
#[[6]]
#[1] 3 2 1
#[[7]]
#[1] "h" "i" "j"
#[[8]]
#[1] 4 6 5
#[[9]]
#[1] 7 8 9
Upvotes: 2