Find the mode value and frequency in R
I'm trying to come up with a function in R that gives the mode value of a column along with the number of times (or frequency) that the value occurs. I want it to exclude missing (or blank) values, and treat ties by showing both values. When there are no repeating values I want it to return the first-appearing value that is found along with its frequency 1.
"Name Color
Drew Blue
Drew Green
Drew Red
Bob Green
Bob Green
Bob Green
Bob Blue
Jim Red
Jim Red
Jim blue
Jim blue
mode of Drew = Blue, 1
mode of Bob = Green, 3
mode of jim = Red, Blue, 2
Here's the function code i have so far, it excludes NAs but does not show both values when there is a tie and does not show frequency. Any help appreciated!
mode - function(x) {
if ( anyNA(x) ) x = x[!is.na(x)]
ux - unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
Topic dplyr data statistics data-cleaning r
Category Data Science