計算每個組內的行數我有一個dataframe,我想計數每個組中的行數。我經常使用aggregate函數對數據求和如下:df2 <- aggregate(x ~ Year + Month, data = df1, sum)現在,我想數數觀察結果,但似乎找不到合適的論據FUN..從直覺上看,我認為情況如下:df2 <- aggregate(x ~ Year + Month, data = df1, count)但是,沒有這樣的運氣。有什么想法嗎?一些玩具數據:set.seed(2)df1 <- data.frame(x = 1:20,
Year = sample(2012:2014, 20, replace = TRUE),
Month = sample(month.abb[1:3], 20, replace = TRUE))
3 回答

慕后森
TA貢獻1802條經驗 獲得超5個贊
df
Year
Month
nrow(df[,df$YEAR == 2007 & df$Month == "Nov"])
aggregate
aggregate(x ~ Year + Month, data = df, FUN = length)
- 3 回答
- 0 關注
- 666 瀏覽
添加回答
舉報
0/150
提交
取消