In R, mean()
and median()
are standard functions which do what you'd expect. mode()
tells you the internal storage mode of the object, not the value that occurs the most in its argument. But is there is a standard library function that implements the statistical mode for a vector (or list)?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- How to insert pictures into each individual bar in
The following function comes in three forms:
method = "mode" [default]: calculates the mode for a unimodal vector, else returns an NA
method = "nmodes": calculates the number of modes in the vector
method = "modes": lists all the modes for a unimodal or polymodal vector
Sorry, I might take it too simple, but doesn't this do the job? (in 1.3 secs for 1E6 values on my machine):
You just have to replace the "round(rnorm(1e6),2)" with your vector.
Could try the following function:
There are multiple solutions provided for this one. I checked the first one and after that wrote my own. Posting it here if it helps anyone:
Lets test it with a few example. I am taking the
iris
data set. Lets test with numeric datawhich you can verify is correct.
Now the only non numeric field in the iris dataset(Species) does not have a mode. Let's test with our own example
EDIT
As mentioned in the comments, user might want to preserve the input type. In which case the mode function can be modified to:
The last line of the function simply coerces the final mode value to the type of the original input.
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.
One more solution, which works for both numeric & character/factor data:
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):