計算每個組內(nèi)的行數(shù)我有一個dataframe,我想計數(shù)每個組中的行數(shù)。我經(jīng)常使用aggregate函數(shù)對數(shù)據(jù)求和如下:df2 <- aggregate(x ~ Year + Month, data = df1, sum)現(xiàn)在,我想數(shù)數(shù)觀察結(jié)果,但似乎找不到合適的論據(jù)FUN..從直覺上看,我認(rèn)為情況如下:df2 <- aggregate(x ~ Year + Month, data = df1, count)但是,沒有這樣的運氣。有什么想法嗎?一些玩具數(shù)據(jù):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貢獻(xiàn)1802條經(jīng)驗 獲得超5個贊
df
Year
Month
nrow(df[,df$YEAR == 2007 & df$Month == "Nov"])
aggregate
aggregate(x ~ Year + Month, data = df, FUN = length)
- 3 回答
- 0 關(guān)注
- 661 瀏覽
添加回答
舉報
0/150
提交
取消