Split a vector into chunks
A one-liner splitting d into chunks of size 20:
split(d, ceiling(seq_along(d)/20))
More details: I think all you need is seq_along()
, split()
and ceiling()
:
> d <- rpois(73,5)> d [1] 3 1 11 4 1 2 3 2 4 10 10 2 7 4 6 6 2 1 1 2 3 8 3 10 7 4[27] 3 4 4 1 1 7 2 4 6 0 5 7 4 6 8 4 7 12 4 6 8 4 2 7 6 5[53] 4 5 4 5 5 8 7 7 7 6 2 4 3 3 8 11 6 6 1 8 4> max <- 20> x <- seq_along(d)> d1 <- split(d, ceiling(x/max))> d1$`1` [1] 3 1 11 4 1 2 3 2 4 10 10 2 7 4 6 6 2 1 1 2$`2` [1] 3 8 3 10 7 4 3 4 4 1 1 7 2 4 6 0 5 7 4 6$`3` [1] 8 4 7 12 4 6 8 4 2 7 6 5 4 5 4 5 5 8 7 7$`4` [1] 7 6 2 4 3 3 8 11 6 6 1 8 4