0

I would like R to output all of the modes for a particular group. Currently, R is giving me just the single mode, which is fine if there is only one mode. However, I would like an output of all modes, if more than one exists. Below is the function I am using for the mode, and I was wondering how it could be modified so that I could do this?

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

2 Answers 2

2

Here's one way in base R -

Mode <- function(x) {
 a <- table(x)
 as.numeric(names(a)[a == max(a)])
}

x <- c(1,2,2,3,3,4)

Mode(x)
[1] 2 3
Sign up to request clarification or add additional context in comments.

Comments

0

You can use 'modeest' package on CRAN. mvf() function stands for (most frequent value(s)) in the package returns mode(s) of given array.

mfv(x) # returns the most frequent value(s) for vector x.

For more information refer to documentation here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.