subset_if {tinycodet}R Documentation

Conditional Sub-setting and In-place Replacement of Unreal Values

Description

The x %[if]% cond operator selects elements from vector/matrix/array x, for which the result of cond(x) returns TRUE.
And the x %[!if]% cond operator selects elements from vector/matrix/array x, for which the result of cond(x) returns FALSE.

The x %unreal =% repl operator modifies all unreal (NA, NaN, Inf, -Inf) values of x with replacement value repl.
Thus,
x %unreal =% repl,
is the same as,
x[is.na(x) | is.nan(x) | is.infinite(x)] <- repl

Usage

x %[if]% cond

x %[!if]% cond

x %unreal =% repl

Arguments

x

a vector, matrix, or array.

cond

a (possibly anonymous) function that returns a logical vector of the same length/dimensions as x.
For example: \(x)x>0.

repl

the replacement value.

Value

For the x %[if]% cond and x %[!if]% cond operators:
The subset_if - operators all return a vector with the selected elements.

For the x %unreal =% repl operator:
The x %unreal =% repl operator does not return any value:
It is an in-place modifier, and thus modifies x directly. The object x is modified such that all NA, NaN, Inf, and -Inf elements are replaced with repl.

See Also

tinycodet_dry

Examples

x <- c(-10:9, NA, NA)
object_with_very_long_name <- matrix(x, ncol=2)
print(object_with_very_long_name)
object_with_very_long_name %[if]% \(x)x %in% 1:10
object_with_very_long_name %[!if]% \(x)x %in% 1:10

x <- c(1:9, NA, NaN, Inf)
print(x)
x %unreal =% 0 # same as x[is.na(x)|is.nan(x)|is.infinite(x)] <- 0
print(x)

[Package tinycodet version 0.5.0 Index]