Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python online compiler
JavaScript online compiler
SQL online compiler
Java online compiler
HTML online compiler
C online compiler
C++ online compiler
C# online compiler
PHP online compiler
Swift online compiler
Kotlin online compiler
TypeScript online compiler
Go online compiler
Rust online compiler
Scala online compiler
Dart online compiler
R online compiler
Ruby online compiler
R Program to Find the Statistical Mode
In this example, we will learn to calculate the statistical mode using the user defined function in R.
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