I'm wanting to calculate a mode value for my dataset (split by other factors, so each group has its own mode value, which I'll probably be doing using dplyr if that makes a difference). I have found this question that discusses a function for locating a modal value.
The thing is, the code pointed to above simply returns the first modal value, so what value is returned varies depending on the order of the dataset. I instead want to create two functions, focussing on the highest and lowest modes in multi-modal distributions.
For example, in the vector
x <- c(4.0, 1.0, 2.2, 2.2, 2.2, 4.0, 0.3, 4.0)
I would want minmode(x)
to return 2.2
, and maxmode(x)
to return 4.0
. Can anyone explain to me how to adapt the code linked above (or create a new function) to do so?