rvectorsplit# R: Split A Vector Into Subvectors with Overlaps and Rebounding

I want to split a parent vector called `v`

into some subvectors with the following conditions:

**Edited for clarity**

Each sub-vector has an equal length

`l`

which is less than the number of the parent vector`v`

.Each sub-vector is unique in its elements' composition and contains consecutive elements.

Elements of a particular sub-vector overlap with elements of previous and subsequent sub-vector.

The same Element must not overlap more than

`l - 1`

times in consecutive sub-vectors as starting element of every sub-vector is arranged in ascending order.Some block elements can be rebounding, in such a way that the start-to-end elements of the parent vector make a sub-vector.

The input should be a vector for the parent vector

`v`

, and an integer for the block length`l`

. While the output should be a list of vectors (not a matrix) such that each sub-vector is output as a vector and the list of all sub-vectors is a list.

For illustration, consider a parent vector of `x1`

to `x10`

with a subvector size of `l = 3`

consecutive elements of its parent vector as follows:

```
x1, x2, x3
x2, x3, x4
x4, x5, x6
x5, x6, x7
x7, x8, x9
x8, x9, x10
x10, x1, x2
```

What I do is form a series of subvectors each with length `l =3`

with starting elements being progressive in nature (`x1, x2 x4, x5, x7, x8, x10`

) and not recursive. The third sub-vector starts from `x4`

and not `x3`

because starting it from `x3`

will make `x3`

overlap `l`

times and not `l - 1`

times at that stage. The same consideration is applied to the 5th and the 7th sub-vector.

For another illustration, consider a parent vector of `x1`

to `x10`

with a subvector size of `l = 4`

consecutive elements of its parent vector as follows:

```
x1, x2, x3, x4
x2, x3, x4, x5
x3, x4, x5, x6
x5, x6, x7, x8
x6, x7, x8, x9
x7, x8, x9, x10
x9, x10, x1, x2
x10, x1, x2, x3
```

What I do is form a series of subvectors each with length `l =3`

with starting elements being progressive in nature (`x1, x2, x3, x5, x6, x7, x9, x10`

) and not recursive. The third sub-vector starts from `x5`

and not `x4`

because starting it from `x4`

will make `x4`

overlap `l`

times and not `l - 1`

times at that stage. The same consideration is applied to the 8th and the 7th sub-vector.

Yet another illustration, consider a parent vector of `x1`

to `x10`

with a subvector size of `l = 5`

consecutive elements of its parent vector as follows:

```
x1, x2, x3, x4, x5
x2, x3, x4, x5, x6
x3, x4, x5, x6, x7
x4, x5, x6, x7, x8
x6, x7, x8, x9, x10
x7, x8, x9, x10, x1
x8, x9, x10, x1, x2
x9, x10, x1, x2, x3
```

Yet another illustration, consider a parent vector of `x1`

to `x10`

with a subvector size of `l = 6`

consecutive elements of its parent vector as follows:

```
x1, x2, x3, x4, x5, x6
x2, x3, x4, x5, x6, x7
x3, x4, x5, x6, x7, x8
x4, x5, x6, x7, x8, x9
x5, x6, x7, x8, x9, x10
x7, x8, x9, x10, x1, x2
x8, x9, x10, x1, x2, x3
x9, x10, x1, x2, x3, x4
x10, x1, x2, x3, x4, x5
```

Yet another illustration, consider a parent vector of `x1`

to `x10`

with a subvector size of `l = 7`

consecutive elements of its parent vector as follows:

```
x1, x2, x3, x4, x5, x6, x7
x2, x3, x4, x5, x6, x7, x8
x3, x4, x5, x6, x7, x8, x9
x4, x5, x6, x7, x8, x9, x10
x5, x6, x7, x8, x9, x10, x1
x6, x7, x8, x9, x10, x1, x2
x8, x9, x10, x1, x2, x3, x4
x9, x10, x1, x2, x3, x4, x5
x10, x1, x2, x3, x4, x5, x6
```

Yet another illustration, consider a parent vector of `x1`

to `x10`

with a subvector size of `l = 8`

consecutive elements of its parent vector as follows:

```
x1, x2, x3, x4, x5, x6, x7, x8
x2, x3, x4, x5, x6, x7, x8, x9
x3, x4, x5, x6, x7, x8, x9, x10
x4, x5, x6, x7, x8, x9, x10, x1
x5, x6, x7, x8, x9, x10, x1, x2
x6, x7, x8, x9, x10, x1, x2, x3
x7, x8, x9, x10, x1, x2, x3, x4
x9, x10, x1, x2, x3, x4, x5, x6
x10, x1, x2, x3, x4, x5, x6, x7
```

Yet another illustration, consider a parent vector of `x1`

to `x10`

with a subvector size of `l = 9`

consecutive elements of its parent vector as follows:

```
x1, x2, x3, x4, x5, x6, x7, x8, x9
x2, x3, x4, x5, x6, x7, x8, x9, x10
x3, x4, x5, x6, x7, x8, x9, x10, x1
x4, x5, x6, x7, x8, x9, x10, x1, x2
x5, x6, x7, x8, x9, x10, x1, x2, x3
x6, x7, x8, x9, x10, x1, x2, x3, x4
x7, x8, x9, x10, x1, x2, x3, x4, x5
x8, x9, x10, x1, x2, x3, x4, x5, x6
x10, x1, x2, x3, x4, x5, x6, x7, x8
```

**What I Need**

I need an `R`

code that gives me the output I want according to the conditions above. You can use `v <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)`

for parent vector input with your choice of `1 < l < length(v)`

in your `R code`

test.

Solution

You can do this with `zoo::rollapply()`

.

```
library(zoo)
fun <- \(v, l) c(v, v[1:(l-1)]) |> # append vector beginning to also get combinations of first and last elements
rollapply(l, \(x) x) |> # apply window
split(seq_along(v)) |> # from matrix to list
(`[`)(seq_along(v) %% l != 0) # remove unwanted subvectors
fun(1:10, 3)
#> $`1`
#> [1] 1 2 3
#>
#> $`2`
#> [1] 2 3 4
#>
#> $`4`
#> [1] 4 5 6
#>
#> $`5`
#> [1] 5 6 7
#>
#> $`7`
#> [1] 7 8 9
#>
#> $`8`
#> [1] 8 9 10
#>
#> $`10`
#> [1] 10 1 2
```

```
fun(1:10, 4)
#> $`1`
#> [1] 1 2 3 4
#>
#> $`2`
#> [1] 2 3 4 5
#>
#> $`3`
#> [1] 3 4 5 6
#>
#> $`5`
#> [1] 5 6 7 8
#>
#> $`6`
#> [1] 6 7 8 9
#>
#> $`7`
#> [1] 7 8 9 10
#>
#> $`9`
#> [1] 9 10 1 2
#>
#> $`10`
#> [1] 10 1 2 3
```

- Retrieving expected data.frame for testthat expectation
- How to use an anonymous function with pipe operator?
- R passing RSelenium driver environment as function argument
- Keep file names merging list with lapply?
- replace strings that contains one another
- Stop parsing out zeros after decimals in ggplot2's annotate
- Plot data.tree coloring and labelling by level
- Extracting the date and time out of a date and time format
- How to reshape a dataframe into wide format with specified column pairs
- Show partial segments removed after limit scales for `geom_pointrange`
- Control ggplot2 legend look without affecting the plot
- Concatenate strings over all rows in a single column of a data frame
- Is there some way to keep variable names from.SD+.SDcols together with non .SD variable names in data.table?
- How to create mutually dependent checkbox columns in a DT table?
- A problem with an stacked, grouped and faceted graph y gg plot
- Installing R on Linux: configure: error: libcurl >= 7.28.0 library and headers are required with support for https
- Create a column conditional on multiple other character and numerical columns
- data.frame to array (2 columns)
- Changing options() in a function environment without changing options() in global environment in R?
- pivot(), group_by() and summarise() with nested data
- Why legend is not showing up in plot layout using patchwork in R?
- in dplyr::mutate, dplyr::starts_with works for .before but not .after?
- Finding optimal cut-off points for dividing a variable into equal groups considering ties in which quantiles do not work
- How to add a legend to hline?
- R Shiny Suppress slider handle for sliderInput until click
- brmultinom brglm2 vifs and dredge dont work
- R, how to get current time miliseconds, without extra information ? (Sys.time(), format("%X"))
- Installing multiple versions of R
- Command to see 'R' path that RStudio is using
- Fast pairwise longest common substring from start