Count number of rows within each group

后端 未结 14 2502
夕颜
夕颜 2020-11-21 05:01

I have a dataframe and I would like to count the number of rows within each group. I reguarly use the aggregate function to sum data as follows:



        
相关标签:
14条回答
  • 2020-11-21 05:29

    dplyr package does this with count/tally commands, or the n() function:

    First, some data:

    df <- data.frame(x = rep(1:6, rep(c(1, 2, 3), 2)), year = 1993:2004, month = c(1, 1:11))
    

    Now the count:

    library(dplyr)
    count(df, year, month)
    #piping
    df %>% count(year, month)
    

    We can also use a slightly longer version with piping and the n() function:

    df %>% 
      group_by(year, month) %>%
      summarise(number = n())
    

    or the tally function:

    df %>% 
      group_by(year, month) %>%
      tally()
    
    0 讨论(0)
  • 2020-11-21 05:35

    If you want to include 0 counts for month-years that are missing in the data, you can use a little table magic.

    data.frame(with(df1, table(Year, Month)))
    

    For example, the toy data.frame in the question, df1, contains no observations of January 2014.

    df1
        x Year Month
    1   1 2012   Feb
    2   2 2014   Feb
    3   3 2013   Mar
    4   4 2012   Jan
    5   5 2014   Feb
    6   6 2014   Feb
    7   7 2012   Jan
    8   8 2014   Feb
    9   9 2013   Mar
    10 10 2013   Jan
    11 11 2013   Jan
    12 12 2012   Jan
    13 13 2014   Mar
    14 14 2012   Mar
    15 15 2013   Feb
    16 16 2014   Feb
    17 17 2014   Mar
    18 18 2012   Jan
    19 19 2013   Mar
    20 20 2012   Jan
    

    The base R aggregate function does not return an observation for January 2014.

    aggregate(x ~ Year + Month, data = df1, FUN = length)
      Year Month x
    1 2012   Feb 1
    2 2013   Feb 1
    3 2014   Feb 5
    4 2012   Jan 5
    5 2013   Jan 2
    6 2012   Mar 1
    7 2013   Mar 3
    8 2014   Mar 2
    

    If you would like an observation of this month-year with 0 as the count, then the above code will return a data.frame with counts for all month-year combinations:

    data.frame(with(df1, table(Year, Month)))
      Year Month Freq
    1 2012   Feb    1
    2 2013   Feb    1
    3 2014   Feb    5
    4 2012   Jan    5
    5 2013   Jan    2
    6 2014   Jan    0
    7 2012   Mar    1
    8 2013   Mar    3
    9 2014   Mar    2
    
    0 讨论(0)
  • 2020-11-21 05:35

    A sql solution using sqldf package:

    library(sqldf)
    sqldf("SELECT Year, Month, COUNT(*) as Freq
           FROM df1
           GROUP BY Year, Month")
    
    0 讨论(0)
  • 2020-11-21 05:37

    Considering @Ben answer, R would throw an error if df1 does not contain x column. But it can be solved elegantly with paste:

    aggregate(paste(Year, Month) ~ Year + Month, data = df1, FUN = NROW)
    

    Similarly, it can be generalized if more than two variables are used in grouping:

    aggregate(paste(Year, Month, Day) ~ Year + Month + Day, data = df1, FUN = NROW)
    
    0 讨论(0)
  • 2020-11-21 05:38

    Current best practice (tidyverse) is:

    require(dplyr)
    df1 %>% count(Year, Month)
    
    0 讨论(0)
  • 2020-11-21 05:39

    If your trying the aggregate solutions above and you get the error:

    invalid type (list) for variable

    Because you're using date or datetime stamps, try using as.character on the variables:

    aggregate(x ~ as.character(Year) + Month, data = df, FUN = length)
    

    On one or both of the variables.

    0 讨论(0)
提交回复
热议问题