Summarize a vector/variable into a single number, either a mean (median) for numeric vectors or the mode for categorical (character, factor, ordered, or logical) vectors. Useful for aggregation.

mean_or_mode(x)

# S3 method for default
mean_or_mode(x)

# S3 method for numeric
mean_or_mode(x)

# S3 method for data.frame
mean_or_mode(x)

median_or_mode(x)

# S3 method for default
median_or_mode(x)

# S3 method for numeric
median_or_mode(x)

# S3 method for data.frame
median_or_mode(x)

Arguments

x

A vector.

Value

A numeric or factor vector of length 1.

See also

Examples

require("datasets") # mean for numerics mean_or_mode(iris)
#> $Sepal.Length #> [1] 5.843333 #> #> $Sepal.Width #> [1] 3.057333 #> #> $Petal.Length #> [1] 3.758 #> #> $Petal.Width #> [1] 1.199333 #> #> $Species #> [1] setosa #> Levels: setosa versicolor virginica #>
mean_or_mode(iris[["Sepal.Length"]])
#> [1] 5.843333
mean_or_mode(iris[["Species"]])
#> [1] setosa #> Levels: setosa versicolor virginica
# median for numerics median_or_mode(iris)
#> $Sepal.Length #> [1] 5.8 #> #> $Sepal.Width #> [1] 3 #> #> $Petal.Length #> [1] 4.35 #> #> $Petal.Width #> [1] 1.3 #> #> $Species #> [1] setosa #> Levels: setosa versicolor virginica #>