The **mode** is the value that occurs most often in a dataset. For example, the mode of the following list of numbers is 12, since that is the value that occurs most often:

10, 12, 12, 12, 13, 17, 19

Unfortunately there is no built-in R function to calculate the mode of a dataset. However, we can easily create a function to do so.

The following function takes a vector as an input and produces the value of the mode as the output:

find_mode <- function(x) { uniques <- unique(x) tab <- tabulate(match(x, uniques)) uniques[tab == max(tab)] }

We can then use this function to the find the mode of a vector of values:

#create a vector of values with only one mode a <- c(1, 4, 6, 6, 7, 8, 9) #find the mode of the vector find_mode(a) #[1] 6 #create a vector of values with two modes b <- c(1, 4, 6, 6, 7, 7, 8, 9) #find the modes of the vector find_mode(b) #[1] 6 7 #create a vector of values with no modes c <- c(1, 4, 6, 7, 8, 9) #find the modes of the vector find_mode(c) #[1] 1 4 6 7 8 9

Notice that the function works whether the vector your provide has no modes, one mode, or more than one mode.