This provides a common interface to extracting and modifying observations for all vector types, regardless of dimensionality. It is an analog to [ that matches vec_size() instead of length().

vec_slice(x, i)

vec_slice(x, i) <- value

Arguments

x

A vector

i

An integer or character vector specifying the positions or names of the observations to get/set.

value

Replacement values.

Examples

x <- sample(10) x
#> [1] 9 10 5 3 8 7 6 4 2 1
vec_slice(x, 1:3)
#> [1] 9 10 5
vec_slice(x, 2L) <- 100 x
#> [1] 9 100 5 3 8 7 6 4 2 1
vec_slice(mtcars, 1:3)
#> mpg cyl disp hp drat wt qsec vs am gear carb #> 1 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 #> 2 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 #> 3 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1