Find and Replace Present Values in mutable_atomic Objects By Reference
Source:R/ma_setv.R
ma_setv.Rd
The ma_setv(x, v rp)
function performs the equivalent of x[which(x == v)] <- rp
but using pass-by-reference semantics.
This is faster than using sb_set(x, i = which(x == v), rp = rp)
.
Inspired by collapse::
setv,
but written in 'C++' through 'Rcpp', with additional safety checks.
Arguments
- x
a mutable_atomic variable.
- v
non-missing (so no
NA
orNaN
) atomic scalar to find.- rp
atomic scalar giving the replacement value.
- invert
Boolean.
IfFALSE
(default), the equivalent ofx[which(x == v()] <- rp
is performed;
IfTRUE
, the equivalent ofx[which(x != v)] <- rp
is performed instead.- NA.safety
Boolean.
just like in which,NA
andNaN
results inx==v
should be ignored, thusNA.safety
isTRUE
by default.
However, if it is known thatx
contains noNA
s orNaN
s, settingNA.safety
toFALSE
will increase performance a bit.
NOTE: SettingNA.safety = FALSE
whenx
does containNA
s orNaN
s, may result in unexpected behaviour.
Value
Returns: VOID. This function modifies the object by reference.
Do not use assignment like x <- ma_setv(x, ...)
.
Since this function returns void, you'll just get NULL
.
Examples
x <- mutable_atomic(c(1:20, NA, NaN))
print(x)
#> [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#> [20] 20 NA NaN
#> mutable_atomic
#> typeof: double
ma_setv(x, 2, 100)
print(x)
#> [1] 1 100 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#> [20] 20 NA NaN
#> mutable_atomic
#> typeof: double