camr_statistic: Compute Univariate Statistic

View source: R/R05-Statistical_tools.R

camr_statisticR Documentation

Compute Univariate Statistic

Description

A function for flexible and robust computation of univariate statistics over a vector of observations. Can be combined with the dplyr::group_by() and dplyr::summarise() functions from the package 'dplyr'.

Usage

camr_statistic(
  x,
  f = length,
  include = NULL,
  exclude = NULL,
  na.rm = TRUE,
  default = NA,
  ...
)

Arguments

x

A vector.

f

A function that computes a univariate statistc (e.g., mean, length).

include

Optional logical vector of matching length to x indicating obserations to include when computing the statistic.

exclude

Optional vector of types of observations in x to exclude (exact matches).

na.rm

Logical; if TRUE, removes NA values.

default

The default value to return in the case of missing data (i.e., no observation over which to compute a statistic).

...

Additional parameters for the function f.

Value

A univariate statistic, or the default value.

Author(s)

Kevin Potter

Examples

# Load in base R data set
data( iris )

# Sepal length
sln <- iris$Sepal.Length
# Number of observations for sepal length
camr_statistics( sln )

# Mean sepal length
camr_statistics( sln, f = mean )
# Define custom function for formatted mean
f_x <- function(x) as.character( round( mean(x), 2 ) )
camr_statistics( sln, f = f_x )

# Petal length
pln <- iris$Petal.Length
# Conditional mean for sepal length when petal length < 3.5
camr_statistics( sln, f = f_x, include = pln < 3.5 )

# Species of iris
spc <- iris$Species
# Isolate species 'virginica'
vrg <- spc == 'virginica'
# No petal lengths less than 3.5 for virginica, so return default
camr_statistics(
  sln[vrg], f = f_x, include = pln[vrg] < 3.5, default = 'No obs'
)

# Compute percentage of species 'setosa'
f_p <- function(x) paste0( round( 100*mean(x == 'setosa') ), '%' )
camr_statistics( spc, f = f_p )
# Exclude 'virginica'
camr_statistics( spc, f = f_p, exclude = 'virginica' )


rettopnivek/camrprojects documentation built on March 26, 2024, 9:17 a.m.