This function either
splits a vector into
n
chunks of equal size (type = 1
),splits a vector into chunks of size
n
(type = 2
).
Arguments
- x
[atomic()`]
A vector of elements.- n
[
integer(1)
]
A number smaller or equallength(x)
.- type
[
1
|2
]
Either1
(default) to splitx
inton
chunks of equal size,or
2
to splitx
into chunks of sizen
.
- strict
[
logical(1)
]
Set toTRUE
to fail iflength(x)
is not a multiple ofn
, orFALSE
(default), else.
See also
Other vector helpers:
check_numeric_vector()
,
check_probability_vector()
,
insert_vector_entry()
,
map_indices()
,
match_numerics()
,
permutations()
,
split_vector_at()
,
subsets()
,
vector_occurrence()
Examples
x <- 1:12
chunk_vector(x, n = 3, type = 1)
#> $`1`
#> [1] 1 2 3 4
#>
#> $`2`
#> [1] 5 6 7 8
#>
#> $`3`
#> [1] 9 10 11 12
#>
chunk_vector(x, n = 3, type = 2)
#> $`1`
#> [1] 1 2 3
#>
#> $`2`
#> [1] 4 5 6
#>
#> $`3`
#> [1] 7 8 9
#>
#> $`4`
#> [1] 10 11 12
#>
try(chunk_vector(x, n = 5, strict = TRUE))
#> Error : Input `n` is bad: Not a multiple of 'length(x)'