vectorized-ox {ox} | R Documentation |
Vectorized ox
Description
Switch the values of the vector by another on specific indices.
Usage
OX(.f, ..., .then = list(...)[[1]], .else = rev(list(...))[[1]])
XO(.f, ..., .then = list(...)[[1]], .else = rev(list(...))[[1]])
Arguments
.f |
( |
... |
arguments passed to the |
.then |
( |
.else |
( |
Details
#' OX
evaluates function .f
which returns a vector of indices which
are then decide which values of .then
are replaced by else
.
.then[!idx] <- .else[!idx]
Consequence of above is that idx = .f(...)
should be a logical
vector or
integer
vector which would be valid indices for .then
and .else
.
This means that .then
and .else
should be of the same length, but there
are two exceptions:
when
.else
is a single value, than this value will replace.then
at returned indices.then[!idx] <- .else
when
.else
isNULL
To invert the switch one can use XO
which is equivalent of
OX(Negate(.f), ..., .then, .else)
.
Value
atomic
or list
. Returned object is a .then
object with elements
replaced by .else
depending on a result of the logical condition.
Examples
# switch values of the vector when condition is true
OX(is.na, c(1, NA, 3), .else = c(2, 2, 2))
# use OX to invert negate the condition
XO(is.na, c(1, NA, 3), .else = c(2, 2, 2))