How To Calculate Mode In R

Many analytical procedures require you to find the mode of a distribution – the value which occurs most commonly. While R does not have a built in function for calculating the mode, this can be easily solved with a little code.

How To Find The Mode in R

Simple answer: we’re going to count the number of times a value occurs within a vector and pick the largest.

``````# calculate mode in r example
test <- c(1,2,3,4,5,5,5,5,3,2,3,1,1,2)

getMode <- function(x) {
keys <- unique(x)
keys[which.max(tabulate(match(x, keys)))]
}``````

How It Works (find mode in R)

Our getMode function splits the problem into two parts.

The first extracts the unique values in our vector and places them in an array called keys.

The second line of code counts the number of time each key occurs and selects the largest one.

Calculate Mode in R – Missing Values

One quirk – if you’re dealing with a sparse data-set where many values are missing, this may throw the calculation off. It could tell you that the most common variable is missing – which may be be true – technically. If you want the largest non-missing value, we’re going to add the omit function.

``````# calculate mode in r
getMode <- function(x) {
keys <- na.omit(unique(x))
keys[which.max(tabulate(match(x, keys)))]
}``````

This is worth dropping into your desktop file or standard library.