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
You can do it like this:
library(dplyr)
df %>%
count(a, b, c) %>%
group_by(a, c) %>%
filter(n == max(n)) %>%
select(a, b, c)
Solution:
# A tibble: 8 x 3
# Groups: a, c [6]
a b c
<fct> <dbl> <fct>
1 a 2 Feb
2 a 1 Feb
3 a 2 Jan
4 a 3 Mar
5 b 3 Mar
6 b 1 Jan
7 b 2 Feb
8 b 3 Feb
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.