Count number of rows within each group

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

    Following @Joshua's suggestion, here's one way you might count the number of observations in your df dataframe where Year = 2007 and Month = Nov (assuming they are columns):

    nrow(df[,df$YEAR == 2007 & df$Month == "Nov"])
    

    and with aggregate, following @GregSnow:

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

提交回复
热议问题