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)

## Arguments

x Object to use Value to compare against or to use as replacement

## Value

• 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

## Details

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 asx.

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.

## Examples

some_na <- c(NA, sample(1:10, 5), NA)
identical(min(some_na, na.rm = TRUE), min_or_na(some_na))
#>  TRUE
all_na <- rep(NA, 5)
min(all_na, na.rm = TRUE)
#> Warning: no non-missing arguments to min; returning Inf#>  Infmin_or_na(all_na)
#>  NA
is_val(some_na, 5)
#>  FALSE FALSE FALSE FALSE FALSE FALSE FALSEsome_na == 5
#>     NA FALSE FALSE FALSE FALSE FALSE    NA
is_val(some_na, NA)
#>  FALSE    NA    NA    NA    NA    NA FALSE
identical(first_elem(letters), head(letters, n = 1L))
#>  TRUEidentical(last_elem(letters), tail(letters, n = 1L))
#>  TRUE
replace_na(some_na, 11)
#>  11  6  2  9  1  7 11replace_na(all_na, 11)
#>  TRUE TRUE TRUE TRUE TRUEreplace_na(1:5, 11)
#>  1 2 3 4 5
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)
#>  FALSE
`