tidyverse / funs

Collection of low-level functions for working with vctrs
Other
34 stars 7 forks source link

Add `mode` to vector functions #59

Open mrcaseb opened 3 years ago

mrcaseb commented 3 years ago

I'd like to ask for a feature that I often use in summarise() calls esp. in grouped data frames.

It would be very helpful to be able to compute the mode (= value that appears most often in a set of values) of a vector. Please correct me if I am wrong but I think there is no base R or dplyr solution for that. However, there are multiple sources that suggest a solution for a vectorized function that computes the mode. The following suggestion is based on a Stack Overflow discussion.

mode <- function(x, na.rm = FALSE) {
   if (isTRUE(na.rm)) x <- x[!is.na(x)]
   ux <- unique(x)
   ux[which.max(tabulate(match(x, ux)))]
}

a <- c(1, 2, 4, 2, 4, 1, 4, NA, NA, NA, NA)

mode(a)
#> [1] NA
mode(a, na.rm = TRUE)
#> [1] 4

b <- c("x", "y", "z", "y", "z", "x", "z", NA_character_, NA_character_, NA_character_, NA_character_)

mode(b)
#> [1] NA
mode(b, na.rm = TRUE)
#> [1] "z"

Created on 2021-03-25 by the reprex package (v1.0.0)