R Program to Find the Statistical Mode

Example: Find Statistical Model in R

# create a vector of marks1
marks1 <- c(56, 88, 75, 87, 73, 69, 47, 59, 61, 92)

# define mode() function
mode = function() {
  
  # calculate mode of marks1  
  return(names(sort(-table(marks1)))[1])
}

# call mode() function
mode()

Output

[1] "47"

In the above example, we have created a function named mode() to calculate the mode of the marks1 vector.

Inside the function, we have used the table() function to create a categorical representation of data with the variable names and the frequency in the form of a table.

We will sort marks1 in descending order and will return the 1st value from the sorted values.

Finally, we have called the mode() function and it returned the most common number in marks1 i.e. 47