This uses the idea of ββpebbles from Lattice graphics and therefore uses the code from the lattice
package to generate the intervals, and then uses the loop to split the original DF into the correct subsets.
I was not exactly sure what is meant by overlap = 1
- I assume that you meant overlapping by 1 pattern / observation. If so, then the code below does this.
OverlapSplit <- function(x, nsplits = 1, overlap = 0) { stopifnot(require(lattice)) N <- seq_len(nr <- nrow(x)) interv <- co.intervals(N, nsplits, overlap / nr) out <- vector(mode = "list", length = nrow(interv)) for(i in seq_along(out)) { out[[i]] <- x[interv[i,1] < N & N < interv[i,2], , drop = FALSE] } out }
What gives:
> OverlapSplit(DF, 2, 2) [[1]] xy 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e 6 6 a [[2]] xy 5 5 e 6 6 a 7 7 b 8 8 c 9 9 d 10 10 e > OverlapSplit(DF) [[1]] xy 1 1 a 2 2 b 3 3 c 4 4 d 5 5 e 6 6 a 7 7 b 8 8 c 9 9 d 10 10 e > OverlapSplit(DF, 4, 1) [[1]] xy 1 1 a 2 2 b 3 3 c [[2]] xy 3 3 c 4 4 d 5 5 e [[3]] xy 6 6 a 7 7 b 8 8 c [[4]] xy 8 8 c 9 9 d 10 10 e
source share