One more solution, which works for both numeric & character/factor data:
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
On my dinky little machine, that can generate & find the mode of a 10M-integer vector in about half a second.
If your data set might have multiple modes, the above solution takes the same approach as which.max, and returns the first-appearing value of the set of modes. To return all modes, use this variant (from @digEmAll in the comments):
Modes <- function(x) {
ux <- unique(x)
tab <- tabulate(match(x, ux))
ux[tab == max(tab)]
}
Answer from Ken Williams on Stack OverflowVideos
How do I find the mode?
Can there be more than one mode?
Which measures of central tendency can I use?
I am looking for the 'mode' from a source where I am not expecting exactly duplicate values. My approach is to treat each sample as a normal distribution with a mean of the sample value and a constant standard deviation. Then take the sum of the PDF's of those distributions as my new PDF, divided by the number of samples. The mode should be the maxima of this function. However, I am finding it difficult to find this maxima, given that the derivative of the pdf of the sum of a number of standard distributions is not easily solvable. Is there a way to solve this analytically, or am I going to have to come up with a numerical solution? Using Newton-Raphson seems like it will have problems, as it tends to just find the nearest zero to your initial guess, and this derivative is going to have a lot of zeroes...
One more solution, which works for both numeric & character/factor data:
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
On my dinky little machine, that can generate & find the mode of a 10M-integer vector in about half a second.
If your data set might have multiple modes, the above solution takes the same approach as which.max, and returns the first-appearing value of the set of modes. To return all modes, use this variant (from @digEmAll in the comments):
Modes <- function(x) {
ux <- unique(x)
tab <- tabulate(match(x, ux))
ux[tab == max(tab)]
}
found this on the r mailing list, hope it's helpful. It is also what I was thinking anyways. You'll want to table() the data, sort and then pick the first name. It's hackish but should work.
names(sort(-table(x)))[1]
The mode is the value occurring with the highest frequency in a data set. If there is a "tie" (with two or more values having equal highest frequencies) you can have more than one mode - bimodal, multimodal etc. However, if every data value occurs exactly one, you generally don't consider that multimodal, you usually just say there's no mode.
In your data set, there is no mode. However, it is possible to transform the data set by truncation, rounding or grouping. For instance if we denoted your (single) variable $x$ and assigned non-overlapping classes like $70.0<x \leq 70.1, 70.1<x \leq 70.2,70.2<x \leq 70.3, 70.3<x \leq 70.4$ then you could argue that your data set has modal class $70.3<x \leq 70.4$.
The modes of a dataset are those values with the greatest frequency of occurrence. This definition allows that there may be more than one mode, when several tie for the position of most frequent.
When all values in the dataset occur with a frequency of "exactly once", they are all modes.