Learn R Programming

sjstats (version 0.11.0)

typical_value: Return the typical value of a vector

Description

This function returns the "typical" value of a variable.

Usage

typical_value(x, fun = c("mean", "median", "mode", "weighted.mean"), ...)

Arguments

x

A variable.

fun

Character vector, naming the function to be applied to numeric x. Currently, "mean", "weighted.mean", "median" and "mode" are supported, which call the corresponding R functions (except "mode", which calls an internal function to compute the most common value).

...

Further arguments, passed down to fun.

Value

The "typical" value of x.

Details

By default, for numeric variables, typical_value() returns the mean value of x (unless changed with the fun-argument). For factors, the reference level and for character vectors, to the most common value (mode) is returned.

Examples

Run this code
# NOT RUN {
data(iris)
typical_value(iris$Sepal.Length)

library(purrr)
map(iris, ~ typical_value(.x))

# example from ?stats::weighted.mean
wt <- c(5,  5,  4,  1) / 15
x <- c(3.7, 3.3, 3.5, 2.8)

typical_value(x, "weighted.mean")
typical_value(x, "weighted.mean", w = wt)

# }

Run the code above in your browser using DataLab