Search code examples
rdplyrgrouping

How to count the number of occurences in a column using dplyr


I'm trying to count the number of occurences in a single column. Here is a snippet of the df I'm working with: enter image description here

Here is the code I have so far:

my_df$day <- weekdays(as.Date(my_df$deadline))
most_common_day <- my_df %>%
arrange(day) %>%
filter(day == "Friday") %>%
select(day)

So the main goal is to get which weekday is the most common. Any suggestions?


Solution

  • There are various ways to count the number of occurrences in R. The basic R method is table():

    table(my_df$day)
    #   Friday    Monday  Saturday    Sunday  Thursday   Tuesday Wednesday 
    #        4         6         8        11         6         5        10
    

    The dplyr approach can be with count():

    count(my_df, day)
    #        day  n
    #1    Friday  4
    #2    Monday  6
    #3  Saturday  8
    #4    Sunday 11
    #5  Thursday  6
    #6   Tuesday  5
    #7 Wednesday 10
    

    You can also use tally() from dplyr but you will also need group_by():

    my_df %>% group_by(day) %>% tally
    #        day  n
    #1    Friday  4
    #2    Monday  6
    #3  Saturday  8
    #4    Sunday 11
    #5  Thursday  6
    #6   Tuesday  5
    #7 Wednesday 10
    

    To get the most common day(s), you can do:

    # when using table()
    names(table(my_df$day))[table(my_df$day) == max(table(my_df$day))]
    #[1] "Sunday"
    
    # when using count()
    count(my_df, day) %>% slice_max(n)
    #     day  n
    #1 Sunday 11
    
    
    # when using tally()
    my_df %>% group_by(day) %>% tally %>% slice_max(n)
    ## A tibble: 1 x 2
    #  day        n
    #  <fct>  <int>
    #1 Sunday    11