Description Usage Arguments Value Functions Examples
View source: R/descriptive-utils.R
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
.
1 2 3 4 5 6 7 8 9 10 11 |
... |
Arguments to a descriptive statistic function |
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.
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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)
sum(x1) # Will be NA
sum_xna(x1) # Will be same as sum(x)
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))
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.