sum_xna: Descriptive statistics for variables with missing values

Description Usage Arguments Value Functions Examples

View source: R/descriptive-utils.R

Description

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.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

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

Examples

 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))

psyntur documentation built on Sept. 15, 2021, 5:07 p.m.