Search code examples
rvectorsplit

R: Split a Vector into Some Overlapping Sub-vectors with Its First Element being Rebounded


I want to split a parent sub-vectors such that these conditions hold:

  1. Each sub-vector has equal and constant length l.
  2. The sub-vectors overlap one another with l -1
  3. Number of sub-vector is given as length(ts) - l + 2
  4. The last sub-vector should include the first element of the parent vector as its last sub-element.

.

ts <- 1:11 # the parent vector
l <- 7 # constant length of sub-vectors to be
m <- length(ts) - l + 1 # number of sub-vector to be
split(t(embed(ts, m))[m:1,], 1:m) 

When I tried this (with m <- length(ts) - l + 1) I got my l = 7 like I want but m = 5 instead of m = 6 like I want.

#$`1`
#[1] 1 2 3 4 5 6 7

#$`2`
#[1] 2 3 4 5 6 7 8

#$`3`
#[1] 3 4 5 6 7 8 9

#$`4`
#[1]  4  5  6  7  8  9 10

#$`5`
#[1]  5  6  7  8  9 10 11

When I tried this (with m <- length(ts) - l + 2) I got m = 6 like I want but l = 6 instead of l = 7.

ts <- 1:11 # the parent vector
l <- 7 # constant length of sub-vectors to be
m <- length(ts) - l + 2 # number of sub-vector to be
split(t(embed(ts, m))[m:1,], 1:m) 

This is what I got

#$`1`
#[1] 1 2 3 4 5 6

#$`2`
#[1] 2 3 4 5 6 7

#$`3`
#[1] 3 4 5 6 7 8

#$`4`
#[1] 4 5 6 7 8 9

#$`5`
#[1]  5  6  7  8  9 10

#$`6`
#[1]  6  7  8  9 10 11

What I Want

#$`1`
#[1] 1 2 3 4 5 6 7

#$`2`
#[1] 2 3 4 5 6 7 8

#$`3`
#[1] 3 4 5 6 7 8 9

#$`4`
#[1]  4  5  6  7  8  9 10

#$`5`
#[1]  5  6  7  8  9 10 11

#$`6`
#[1]  6  7  8  9 10 11  1

Solution

  • Here you go:

    ts <- 1:11 # the parent vector
    l <- 7 # constant length of sub-vectors to be
    m <- length(ts) - l + 2 # number of sub-vector to be
    
    library(magrittr)
    lapply(1:m, function(i){
      c(0, rep(ts, 4)) %>% # Adding leading zero (cut in first iter; repeat a few times to not run out if we increase m
        tail(-i) %>% # Cut first i elements (including added zero)
        head(l) # Retain first l of the remaining part
    })