The mode in R is that the value that has highest number of occurrences during a set of knowledge . Unlike mean and median, mode can deal with both numeric as well as categorical data.

R doesn’t have a typical in-built function to calculate mode. So we create a user function to calculate mode of a knowledge set in R. This function takes the vector as input and provides the mode value as output.

Advantages of Mode in statistics

  • Easy to calculate.
  • It will not get affected by the extreme values or outliers.
  • helps to identify the frequency distribution.
  • helps to analyse quantitative data.
  • can be computed using frequency table.

Mode in R programming language

As I stated above, R does not have an default function to calculate the mode of the values. So, we have to create a user defined function to calculate the mode.

Let’s see how it works.

#Creating an user defined function which calculates the mode of data
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}
#Creates a vector
df <- c(22,34,32,22,34,32,34,54,34,65,45,43,43,23,43)
#Returns the mode of the values 
getmode(df)
34

Awesome!!! in the above section, we have created a user defined function, because of no in-built function in R to calculate the mode of the data. Then we have passed the input to our function which returned the mode of the input values.

Mode in R with Character data

Well, in the above lines, I have mentioned that mode in R can be applicable to character data as well. So, let’s see how it works.

#Creating an user defined function which calculates the mode of data
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}
df <- c('and','if','you','if','me','me','if','and','you')
getmode(df)
"if"

Fantastic!!!

Our mode function has returned the most occurred values in the data i.e. ‘if’. That’s the beauty of the mode function.

Mode of the values in the dataset

Well, till now we have used the mode function to get the mode value of the data. Now, we are going to apply the function to the values present in the dataset.

#Imports the data
df<- datasets::iris
df
       Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1            5.1         3.5          1.4         0.2     setosa
2            4.9         3.0          1.4         0.2     setosa
3            4.7         3.2          1.3         0.2     setosa
4            4.6         3.1          1.5         0.2     setosa
5            5.0         3.6          1.4         0.2     setosa
6            5.4         3.9          1.7         0.4     setosa
7            4.6         3.4          1.4         0.3     setosa
8            5.0         3.4          1.5         0.2     setosa
9            4.4         2.9          1.4         0.2     setosa
10           4.9         3.1          1.5         0.1     setosa

We are using the iris dataset for this purpose as shown above.

#defines the function 
#Creating an user defined function which calculates the mode of data
getmode <- function(v) {
   uniqv <- unique(v)
   uniqv[which.max(tabulate(match(v, uniqv)))]
}
getmode(df$Sepal.Length)
5
getmode(df$Sepal.Width)
3
getmode(df$Petal.Length)
1.4
getmode(df$Petal.Width)
0.2
getmode(df$Species)
setosa
Levels: setosa versicolor virginica

we have defined a mode function and it returned all the values whose occurrence is higher than other values in the given input iris dataset.

Wrapping Up

It is the worth that has the very best frequency within the given data set. the info set may haven’t any mode if the frequency of all data points is that the same. Also, we will have quite one mode if we encounter two or more data points having an equivalent frequency.

That’s all for now, Happy R!!!

More read: R documentation

Categorized in: