1

I need to find the most common values for the unique names in column afor 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
7
  • If you could explain what you are doing and why, that would help. Feb 19, 2020 at 11:53
  • I have already added information, sorry.
    – Zizou
    Feb 19, 2020 at 11:59
  • Your Modes function returns more than one element if there are ties. Try ux[tab == max(tab)][1] as a (somewhat hackish) fix.
    – dario
    Feb 19, 2020 at 12:02
  • 1
    @dario It intentionally returns multiple values, the question already contains a simpler variant that returns a single value. Feb 19, 2020 at 12:03
  • 1
    Ok. I see. So do you want them returned as a list (list(ux[tab == max(tab)])) ? Or what do you want to do with these multiple values?
    – dario
    Feb 19, 2020 at 12:12

1 Answer 1

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 

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.