Utility functions
Several utility functions exported for convenience.
min_or_na(x) max_or_na(x) is_val(x, val) not_val(x, val) is_true(x) is_false(x) last_elem(x) first_elem(x) replace_na(x, val, ...)
x |
Object to use |
val |
Value to compare against |
... |
Forwarded to other methods |
The two functions min_or_na()
and max_or_na()
overcome a design choice
of base::min()
(or base::max()
) that can yield undesirable results. If called on a vector of all missing values with
na.rm = TRUE,
Inf(and
-Infrespectively) are returned. This is changed to returning a missing value of the same type as
x'.
The functions is_val()
and not_val()
(as well as analogously
is_true()
and is_false()
) return logical vectors of the same length as
the value passed as x
, with non-base R semanticists of comparing against
NA
: instead of returning c(NA, TRUE)
for c(NA, 5) == 5
, is_val()
will return c(FALSE TRUE)
. Passing NA
as val
might lead to unintended
results but no warning is thrown.
Finally, first_elem()
and last_elem()
has the same semantics as
utils::head()
and utils::tail()
with n = 1L
and replace_na()
will
replace all occurrences of NA
in x
with val
and can be called on both
objects inheriting from data.table
in which case internally
data.table::setnafill()
is called or other objects.
min_or_na()
/max_or_na()
: scalar-valued extrema of a vector
is_val()
/not_val()
/is_true()
/is_false()
: Logical vector of the
same length as the object passed as x
first_elem()
/last_elem()
: single element of the object passed as x
replace_na()
: modified version of the object passed as x
some_na <- c(NA, sample(1:10, 5), NA) identical(min(some_na, na.rm = TRUE), min_or_na(some_na)) all_na <- rep(NA, 5) min(all_na, na.rm = TRUE) min_or_na(all_na) is_val(some_na, 5) some_na == 5 is_val(some_na, NA) identical(first_elem(letters), head(letters, n = 1L)) identical(last_elem(letters), tail(letters, n = 1L)) replace_na(some_na, 11) replace_na(all_na, 11) replace_na(1:5, 11) tbl <- ts_tbl(a = 1:10, b = hours(1:10), c = c(NA, 1:5, NA, 8:9, NA)) res <- replace_na(tbl, 0) identical(tbl, res)
Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.