Reputation: 11480
How can i transform the first "long" list l:
l <- list()
n=1
for (i in 1:9) {
l[[length(l)+1]] <- n:(n+2)
n=n+3
}
into a wider but shorter list of lists lol: In the next code segment im breaking after every 3 Elements of l.
lol <- list()
lol[[1]] <- list(1:3,4:6,7:9)
lol[[2]] <- list(10:12,13:15,16:18)
lol[[3]] <- list(19:21,22:24,25:27)
Any ideas? What if i want to break after every 2 Elements of l.
Upvotes: 2
Views: 170
Reputation: 214927
You can split it by a vector 1,1,1,2,2,2,3,3,3
:
n = 3 # the length of each sub list
split(l, (seq_along(l) - 1) %/% n)
identical(lol, setNames(split(l, (seq_along(l) - 1) %/% 3), NULL))
# [1] TRUE
Upvotes: 2