# Computing the mode in R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In R there isn’t a function for computing the mode. This statistic is not often used but it is very useful for categorical and discrete data.**My R Nightmares**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

The mode is defined as “the most common value occurring in a set of observations.” Mathematically, for numerical data, the mode is the centre of order zero mode = arg min_m sum [x_i – m]^0, where 0^0 is defined as equal to 0.

This definition is not complete because in a set of data there can be one or many or no mode. For example: in a set with 10 apples, 5 pears and 2 bananas the mode is apple, in a set with 5 apples, 5 pears and 2 bananas the modes are apple and pear, in a set with 5 apples, 5 pears and 5 bananas there is no mode. This is shown in the figure below.

Hence a function that computes the mode needs to return one or many or no value. Furthermore, the values returned can be either characters or numbers (in case of discrete data). My solution is this:

Mode = function(x){

ta = table(x)

tam = max(ta)

if (all(ta == tam))

mod = NA

else

if(is.numeric(x))

mod = as.numeric(names(ta)[ta == tam])

else

mod = names(ta)[ta == tam]

return(mod)

}

Let’s see how it works for

One mode

fruit = c(rep(“apple”, 10), rep(“pear”, 5), rep(“banana”, 2))

Mode(fruit)

# [1] “apple”

Two modes

fruit2 = c(rep(“apple”, 5), rep(“pear”, 5), rep(“banana”, 2))

Mode(fruit2)

# [1] “apple” “pear”

No mode

fruit3 = c(rep(“apple”, 5), rep(“pear”, 5), rep(“banana”, 5))

Mode(fruit3)

# [1] NA

Works fine for nominal data. Let’s check for

One mode

count1 = c(rep(1, 10), rep(2, 5), rep(3, 2))

Mode(count1)

# [1] 1

Two modes

count2 = c(rep(1, 5), rep(2, 5), rep(3, 2))

Mode(count2)

# [1] 1 2

No mode

count3 = c(rep(1, 5), rep(2, 5), rep(3, 5))

Mode(count3)

# [1] NA

ta = table(x)

tam = max(ta)

if (all(ta == tam))

mod = NA

else

if(is.numeric(x))

mod = as.numeric(names(ta)[ta == tam])

else

mod = names(ta)[ta == tam]

return(mod)

}

Let’s see how it works for

**nominal data**:One mode

fruit = c(rep(“apple”, 10), rep(“pear”, 5), rep(“banana”, 2))

Mode(fruit)

# [1] “apple”

Two modes

fruit2 = c(rep(“apple”, 5), rep(“pear”, 5), rep(“banana”, 2))

Mode(fruit2)

# [1] “apple” “pear”

No mode

fruit3 = c(rep(“apple”, 5), rep(“pear”, 5), rep(“banana”, 5))

Mode(fruit3)

# [1] NA

Works fine for nominal data. Let’s check for

**numerical data**:One mode

count1 = c(rep(1, 10), rep(2, 5), rep(3, 2))

Mode(count1)

# [1] 1

Two modes

count2 = c(rep(1, 5), rep(2, 5), rep(3, 2))

Mode(count2)

# [1] 1 2

No mode

count3 = c(rep(1, 5), rep(2, 5), rep(3, 5))

Mode(count3)

# [1] NA

To

**leave a comment**for the author, please follow the link and comment on their blog:**My R Nightmares**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.