These functions transform your variable by reference, with no copies being made. It is advisable to only use these if you know what you are doing.
set_abs(x)set_floor(x)
set_ceiling(x)
set_trunc(x)
set_exp(x)
set_sqrt(x)
set_change_sign(x)
set_round(x, digits = 0)
set_log(x, base = exp(1))
set_pow(x, y)
set_add(x, y)
set_subtract(x, y)
set_multiply(x, y)
set_divide(x, y)
The exact same object with no copy made, just transformed.
A numeric vector.
Number of digits to round to.
Logarithm base.
A numeric vector.
These functions are particularly useful for situations
where you have made a copy and then
wish to perform further operations without creating more copies.
NA
and NaN
values are ignored though in some instances NaN
values may
be replaced with NA
.
These functions will not work on any classed objects, meaning they
only work on standard integer and numeric vectors and matrices.
A copy is only made in certain instances, e.g. when passing an integer vector
to set_log()
. A warning will always be thrown in this instance alerting the user
to assign the output to an object because x
has not been updated by reference.
To ensure consistent and expected outputs, always assign the output to the same object,
e.g.
x <- set_log(x)
(do this)
set_log(x)
(don't do this)
x2 <- set_log(x)
(Don't do this either)
No copy is made here unless x is an integer vector.
library(cheapr)
library(bench)
x <- rnorm(2e05)
options(cheapr.cores = 2)
mark(
base = exp(log(abs(x))),
cheapr = set_exp(set_log(set_abs(x)))
)
options(cheapr.cores = 1)
Run the code above in your browser using DataLab