| typical_value {sjstats} | R Documentation |
This function returns the "typical" value of a variable.
typical_value(x, fun = "mean", weight.by = NULL, ...)
x |
A variable. |
fun |
Character vector, naming the function to be applied to
|
weight.by |
Vector of weights that will be applied to weight all cases.
Must be a vector of same length as the input vector. Default is
|
... |
Further arguments, passed down to |
By default, for numeric variables, typical_value() returns the
mean value of x (unless changed with the fun-argument).
For factors, the reference level is returned or the most common value
(if fun = "mode"), unless fun is a named vector. If
fun is a named vector, specify the function for numeric
and categorical variables as element names, e.g.
fun = c(numeric = "median", factor = "mean"). In this case,
factors are converted to numeric values (using to_value)
and the related function is applied. You may abbreviate the names
fun = c(n = "median", f = "mean"). See also 'Examples'.
For character vectors the most common value (mode) is returned.
The "typical" value of x.
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", weight.by = wt) # for factors, return either reference level or mode value set.seed(123) x <- sample(iris$Species, size = 30, replace = TRUE) typical_value(x) typical_value(x, fun = "mode") # for factors, use a named vector to apply other functions than "mode" map(iris, ~ typical_value(.x, fun = c(n = "median", f = "mean")))