centre: Return the median value or the modal value of a vector,...

View source: R/helpers.R

centreR Documentation

Return the median value or the modal value of a vector, depending on whether the vector is numeric or a factor.

Description

Return the median value or the modal value of a vector, depending on whether the vector is numeric or a factor.

Usage

centre(x, ...)

## S3 method for class 'numeric'
centre(x, FUN = median, na.rm = TRUE, ...)

## S3 method for class 'factor'
centre(x, FUN, na.rm = TRUE, ...)

Arguments

x

an object for which a method has been defined, or a numeric vector containing the values whose median is to be computed.

...

potentially further arguments for methods; not used in the default method.

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

Value

the median value for objects of the class integer or double. The modal value for objects of class factor.

Examples

x <- c(-3, -2, 0, NA, 1, 1, 3)
centre(x) # .5 (default is median(x))
centre(x, FUN = mean) # 0
centre(x, FUN = raster::modal) # 1
y <- c(-3, -2, 0, 1, 1, 3)
y <- as.factor(y)
centre(y) # the reference level
centre(y, FUN = raster::modal) # the most frequent (modal) value
library(sf)
data(centro_2015)
centre(centro_2015$padrao) # the reference level
centre(centro_2015$padrao, FUN = raster::modal) # the most frequent value

lfpdroubi/appraiseR documentation built on April 14, 2024, 10:27 p.m.