从数据框中,是否有一种简单的方法可以同时聚合(sum、、mean等max)多个变量?
sum
mean
max
以下是一些示例数据:
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变量呢?
x1
x2
df2
### aggregate variables by year month df2=aggregate(x1 ~ year+month, data=df1, sum, na.rm=TRUE) head(df2)
这个year()功能是从哪里来的?
year()
你也可以使用这个reshape2包来完成这个任务:
reshape2
require(reshape2) df_melt <- melt(df1, id = c("date", "year", "month")) dcast(df_melt, year + month ~ variable, sum) # year month x1 x2 1 2000 1 -80.83405 -224.9540159 2 2000 2 -223.76331 -288.2418017 3 2000 3 -188.83930 -481.5601913 4 2000 4 -197.47797 -473.7137420 5 2000 5 -259.07928 -372.4563522