Site icon R-bloggers

R Program to Find the Statistical Mode

[This article was first published on R feed, 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.

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

To leave a comment for the author, please follow the link and comment on their blog: R feed.

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.