8
每组汇总/汇总多个变量(例如,总和,均值)
从数据帧,是否有聚集(一个简单的方法sum,mean,max同时等c)中多个变量? 以下是一些示例数据: library(lubridate) days = 365*2 date = seq(as.Date("2000-01-01"), length = days, by = "day") year = year(date) month = month(date) x1 = cumsum(rnorm(days, 0.05)) x2 = cumsum(rnorm(days, 0.05)) df1 = data.frame(date, year, month, x1, x2) 我想同时按年份和月份汇总数据框中的x1和x2变量df2。以下代码汇总了x1变量,但是也可以同时汇总x2变量吗? ### aggregate variables by year month df2=aggregate(x1 ~ year+month, data=df1, sum, na.rm=TRUE) head(df2) …