vec_as_index {vctrs} | R Documentation |
This provides a means of standardizing common indexing methods such as integer, character or logical indexing. The input is a vector of one of these three types, and the output is always an integer vector that can be used for subsetting.
vec_as_index(i, n, names = NULL)
i |
An integer, character or logical vector specifying the positions or
names of the observations to get/set.
Specify |
n |
A single integer representing the total size of the
object that |
names |
If |
An integer vector that can be used as an index in a subsetting operation.
x <- array(1:6, c(2, 3)) dimnames(x) <- list(c("r1", "r2"), c("c1", "c2", "c3")) # The most common use case validates row indices vec_as_index(1, vec_size(x)) # Negative indices can be used to index from the back vec_as_index(-1, vec_size(x)) # Character vectors can be used if `names` are provided vec_as_index("r2", vec_size(x), rownames(x)) # You can also construct an index for dimensions other than the first vec_as_index(c("c2", "c1"), ncol(x), colnames(x))