聚合多个小计?

时间:2013-02-11 21:09:54

标签: r reshape2

有没有办法用reshape2汇总多个小计?例如。对于空气质量数据集

require(reshape2)
require(plyr)
names(airquality) <- tolower(names(airquality))
aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE)
aqm <- subset(aqm, month %in% 5:6 & day %in% 1:7)

我可以为每个月制作一个小计列,其中包含该月内所有变量的平均值:

dcast(aqm, day ~ month+variable, mean, margins = "variable")
  day 5_ozone 5_solar.r 5_wind 5_temp 5_(all) 6_ozone 6_solar.r
1   1      41       190    7.4     67  76.350     NaN       286
2   2      36       118    8.0     72  58.500     NaN       287
3   3      12       149   12.6     74  61.900     NaN       242
4   4      18       313   11.5     62 101.125     NaN       186
5   5     NaN       NaN   14.3     56  35.150     NaN       220
6   6      28       NaN   14.9     66  36.300     NaN       264
7   7      23       299    8.6     65  98.900      29       127
  6_wind 6_temp   6_(all)
1    8.6     78 124.20000
2    9.7     74 123.56667
3   16.1     67 108.36667
4    9.2     84  93.06667
5    8.6     85 104.53333
6   14.3     79 119.10000
7    9.7     82  61.92500

我还可以为每个变量创建一个小计列,该列具有该变量中所有月份的平均值:

dcast(aqm, day ~ variable+month, mean, margins = "month")
  day ozone_5 ozone_6 ozone_(all) solar.r_5 solar.r_6 solar.r_(all)
1   1      41     NaN          41       190       286         238.0
2   2      36     NaN          36       118       287         202.5
3   3      12     NaN          12       149       242         195.5
4   4      18     NaN          18       313       186         249.5
5   5     NaN     NaN         NaN       NaN       220         220.0
6   6      28     NaN          28       NaN       264         264.0
7   7      23      29          26       299       127         213.0
  wind_5 wind_6 wind_(all) temp_5 temp_6 temp_(all)
1    7.4    8.6       8.00     67     78       72.5
2    8.0    9.7       8.85     72     74       73.0
3   12.6   16.1      14.35     74     67       70.5
4   11.5    9.2      10.35     62     84       73.0
5   14.3    8.6      11.45     56     85       70.5
6   14.9   14.3      14.60     66     79       72.5
7    8.6    9.7       9.15     65     82       73.5

有没有办法告诉reshape2在一个命令中计算两组小计?此命令已关闭,在总计中添加,但省略了每月小计:

dcast(aqm, day ~ variable+month, mean, margins = c("variable", "month"))

1 个答案:

答案 0 :(得分:1)

如果我的问题正确,您可以使用

acast(aqm, day ~ variable ~ month, mean, margins = c("variable", "month"))[,,'(all)']

acast为您提供每个月每个变量的每日摘要。总聚合“切片”([,,'(all)'])每天有一行,每个变量的列(所有月份的平均值)和每天平均的'(全部)列,所有所有月份的变量。

这是你需要的吗?