Count number of rows within each group

后端 未结 14 2508
夕颜
夕颜 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:49

    You can use by functions as by(df1$Year, df1$Month, count) that will produce a list of needed aggregation.

    The output will look like,

    df1$Month: Feb
         x freq
    1 2012    1
    2 2013    1
    3 2014    5
    --------------------------------------------------------------- 
    df1$Month: Jan
         x freq
    1 2012    5
    2 2013    2
    --------------------------------------------------------------- 
    df1$Month: Mar
         x freq
    1 2012    1
    2 2013    3
    3 2014    2
    > 
    

提交回复
热议问题