From this
x <- 1:10
block.size <- 3L
I would like to generate a sequence of consecutive and overlapping blocks of length 3 like so:
1,2,3
2,3,4
3,4,5
4,5,6
5,6,7
6,7,8
7,8,9
8,9,10
I found this nice answer which works with characters.
I can think of a loop to do this, but I would definitely prefer a more concise and vectorized way if possible. Here is my take.
block.nums <- length(x)-len+1
blocks <- vector(mode = "list", length = block.nums)
for (i in 1:block.nums) {
blocks[[i]] <- i:(i+block.size-1)
}