vec_chop() provides an efficient method to repeatedly slice a vector. It captures the pattern of map(indices, vec_slice, x = x).

vec_chop(x, indices = NULL)

Arguments

x

A vector

indices

A list of index values to slice x with, or NULL. Each element of the list must be an integer, character or logical vector that would be valid as an index in vec_slice(). If NULL, x is split into its individual elements, equivalent to using an indices of as.list(vec_seq_along(x)).

Value

A vector of type list_of<vec_ptype(x)> and size vec_size(indices) or, if indices == NULL, vec_size(x).

Examples

vec_chop(1:5)
#> <list_of<integer>[5]> #> [[1]] #> [1] 1 #> #> [[2]] #> [1] 2 #> #> [[3]] #> [1] 3 #> #> [[4]] #> [1] 4 #> #> [[5]] #> [1] 5 #>
vec_chop(1:5, list(1, 1:2))
#> <list_of<integer>[2]> #> [[1]] #> [1] 1 #> #> [[2]] #> [1] 1 2 #>
vec_chop(mtcars, list(1:3, 4:6))
#> <list_of< #> data.frame< #> mpg : double #> cyl : double #> disp: double #> hp : double #> drat: double #> wt : double #> qsec: double #> vs : double #> am : double #> gear: double #> carb: double #> > #> >[2]> #> [[1]] #> mpg cyl disp hp drat wt qsec vs am gear carb #> Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 #> Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 #> Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 #> #> [[2]] #> mpg cyl disp hp drat wt qsec vs am gear carb #> Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 #> Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 #> Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 #>