vec_as_location {vctrs} | R Documentation |
These helpers provide a means of standardizing common indexing methods such as integer, character or logical indexing.
vec_as_location()
accepts integer, character, or logical vectors
of any size. The output is always an integer vector that is
suitable for subsetting with [
or vec_slice()
. It might be a
different size than the input because negative selections are
transformed to positive ones and logical vectors are transformed
to a vector of indices for the TRUE
locations.
vec_as_location2()
accepts a single number or string. It returns
a single location as a integer vector of size 1. This is suitable
for extracting with [[
.
vec_as_location(
i,
n,
names = NULL,
...,
missing = c("propagate", "error"),
arg = caller_arg(i),
call = caller_env()
)
num_as_location(
i,
n,
...,
missing = c("propagate", "error"),
negative = c("invert", "error", "ignore"),
oob = c("error", "extend"),
zero = c("remove", "error", "ignore"),
arg = caller_arg(i),
call = caller_env()
)
vec_as_location2(
i,
n,
names = NULL,
...,
missing = c("error", "propagate"),
arg = caller_arg(i),
call = caller_env()
)
num_as_location2(
i,
n,
...,
negative = c("error", "ignore"),
missing = c("error", "propagate"),
arg = caller_arg(i),
call = caller_env()
)
i |
An integer, character or logical vector specifying the
locations or names of the observations to get/set. Specify
|
n |
A single integer representing the total size of the
object that |
names |
If |
... |
These dots are for future extensions and must be empty. |
missing |
Whether to throw an |
arg |
The argument name to be displayed in error messages when
|
call |
The execution environment of a currently
running function, e.g. |
negative |
Whether to throw an |
oob |
If |
zero |
Whether to |
vec_as_location()
returns an integer vector that can be used
as an index in a subsetting operation. vec_as_location2()
returns an integer of size 1 that can be used a scalar index for
extracting an element.
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_location(1, vec_size(x))
# Negative indices can be used to index from the back
vec_as_location(-1, vec_size(x))
# Character vectors can be used if `names` are provided
vec_as_location("r2", vec_size(x), rownames(x))
# You can also construct an index for dimensions other than the first
vec_as_location(c("c2", "c1"), ncol(x), colnames(x))