# [QUERY] Using Mode in R?

Tagged:

Viewing 5 posts - 1 through 5 (of 5 total)
• Author
Posts
• #3129
Lekshman
Participant

For calculation of mean and median we have built-in functions in R.
But for mode calculations we do not have any built-in function.
I had googled and found a piece of code ( code attached below) that can be used to find mode, however, it has a lot of limitations. does anyone know any other easier way to calculate mode in R? like a packadge installation.

#3130
Lekshman
Participant

This is the Code i found on the net, But the limitation of the below code is that, CalMode(1:5) gives 1 and CalMode(5:1) gives 5 so when all the numbers have same freq, the code fails.

Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}

#3209
Changyue Song
Keymaster

try


Mode = function(x) {
tab = table(x)
md = which(tab == max(tab))
return(as.numeric(names(md)))
}

#3240
William Schmitz
Participant

I am trying to find the mode in R for a homework assignment (ISyE 412).  By inspection, I see that the data in the vector is trimodal.  This data vector is small.  I tried the above suggested method from the post as well as the website https://www.tutorialspoint.com/r/r_mean_median_mode.htm.  This yielded only one answer.

My question is: how do we get bimodal and trimodal results when using R?  With very large data sets, the above method may be problematic.

I appreciate any suggestions?

#3243
Wenjun Zhu
Participant

Reply to William Schmitz of #3240:

When the data set is small, we can also try in a shorter and simpler way:

table(x)[table(x)==max(table(x))]

When the data set is very large, we can apply MapReduce and Hadoop in R. Attached below are my personal points. Hope can help you!

###### Attachments:
You must be logged in to view attached files.
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.