Most descriptive statistic function like base::sum(), base::mean(), stats::median(), etc., do not skip NA values when computing the results and so always return NA if there is at least one NA in the input vector. The NA values can be skipped always by setting the na.rm argument to TRUE. While this is simply to do usually, in some cases, such as when a function is being passed to another function, setting na.rm = TRUE in that function requires creating a new anonymous function. The functions here, which all end in _xna, are wrappers to common statistics functions, but with na.rm = TRUE.

sum_xna(...)

mean_xna(...)

median_xna(...)

iqr_xna(...)

sd_xna(...)

var_xna(...)

Arguments

...

Arguments to a descriptive statistic function

Value

A numeric vector, usually with one element, that provides the result of a descriptive statistics function applied to a vector after the NA values have been removed.

Functions

  • mean_xna(): The arithmetic mean for vectors with missing values.

  • median_xna(): The median for vectors with missing values.

  • iqr_xna(): The interquartile range for vectors with missing values.

  • sd_xna(): The standard deviation for vectors with missing values.

  • var_xna(): The variance for vectors with missing values.

Examples

set.seed(10101)
# Make a vector of random numbers
x <- runif(10, min = 10, max = 20)
# Concatenate with a NA value
x1 <- c(NA, x)
sum(x)
#> [1] 159.472
sum(x1) # Will be NA
#> [1] NA
sum_xna(x1) # Will be same as sum(x)
#> [1] 159.472
stopifnot(sum_xna(x1) == sum(x))
stopifnot(mean_xna(x1) == mean(x))
stopifnot(median_xna(x1) == median(x))
stopifnot(iqr_xna(x1) == IQR(x))
stopifnot(sd_xna(x1) == sd(x))
stopifnot(var_xna(x1) == var(x))