I need to find the most common values for the unique names in column a
for individual month. I know that the topic was already and I found a solution here: Is there a built-in function for finding the mode?, but I have a problem with multiple mode.
Solutions below:
Mode <- function(x) {
ux <- unique(x)
ux[which.max(tabulate(match(x, ux)))]
}
df<-data.frame(a=rep(c("a","b"),each=5),b=c(2,1,2,2,3,3,1,1,2,3),
c = c("Feb","Feb","Jan","Jan","Mar","Mar","Jan","Jan","Feb","Feb"))
df %>% group_by(a,c) %>% summarise(d=Mode(b))
# A tibble: 6 x 3
# Groups: a [2]
a c d
<fct> <fct> <dbl>
1 a Feb 2
2 a Jan 2
3 a Mar 3
4 b Feb 2
5 b Jan 1
6 b Mar 3
#When I want use:
Modes <- function(x) {
ux <- unique(x)
tab <- tabulate(match(x, ux))
ux[tab == max(tab)]
}
df %>% group_by(a,c) %>% summarise(d=Modes(b))
#I get:
Error: Column `d` must be length 1 (a summary value), not 2
I expected:
1 a Feb 2
2 a Feb 1
3 a Jan 2
4 a Mar 3
5 b Feb 2
6 b Feb 3
7 b Jan 1
8 b Mar 3