Videos
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...
value that appears most often in a set of data
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]
I have a table of data from which I need to find the mode of the data within a single column. I cannot use the mode function because the numbers are of times of the day, and I need to find the mode within an hour. (Even though some numbers are not the exact same, they would still fall within the hour, but the mode function would not recognize this.) I figure the best way to analyze the column is to graph it in a bar chart. How do I do this?
Any help is appreciated!