重塑和均值计算

Reshape and mean calculation

我有一整年沿着海拔梯度收集的气候数据。形状是这样的:

clim <- read.table(text="alti    year    month    week    day    meanTemp    maxTemp    minTemp
350     2011    aug.     31      213    10          14         6
350     2011    aug.     31      214    12          18         6
350     2011    aug.     31      215    10          11         9
550     2011    aug.     31      213    8           10         6
550     2011    aug.     31      214    10          12         8
550     2011    aug.     31      215    8           9          7
350     2011    sep.     31      244    9           10         8
350     2011    sep.     31      245    11          12         10
350     2011    sep.     31      246    10          11         9
550     2011    sep.     31      244    7.5         9          6
550     2011    sep.     31      245    8           10         6
550     2011    sep.     31      246    8.5         9          8", header=TRUE)

我正在尝试重塑这些数据,以便每个高度只有一行,并计算每个月和全年的平均数据。要是能变成那样就好了:

alti    mean_year(meanTemp)   mean_year(maxTemp)   mean_aug.(meanTemp)   mean_aug.(maxTemp)   mean_sep.(meanTemp)   [...]
350     10.333                12.667               10.667                14.3                 10                     ...
550     8.333                 9.833                8.667                 10.333               7.766                  ...

有执行此重塑和计算的想法吗?

您可以使用 data.table 和 dcast:

library(data.table)

setDT(clim)

merge(

clim[, list("mean_temp_mean_year" = mean(meanTemp), "max_temp_mean_year" = mean(maxTemp)), by = alti]
,
dcast(clim[, list("mean_temp_mean" = mean(meanTemp), "max_temp_mean" = mean(maxTemp)), by = c("alti","month")], alti ~ month, value.var = c("mean_temp_mean","max_temp_mean"))
,
by = "alti")

我换了一些变量的名字,你的顺序不是很完美,但是可以reordered/renamed之后

要获取月份或年份的平均值,您可以使用 aggregate 后跟 reshape

两个聚合可以分开计算,然后merge放在一起:

mon <- aggregate(cbind(meanTemp, maxTemp) ~ month + alti, data=clim, FUN=mean)
mon.wide <- reshape(mon, direction='wide', timevar='month', idvar='alti')

yr <- aggregate(cbind(meanTemp, maxTemp) ~ year + alti, data=clim, FUN=mean)
yr.wide <- reshape(yr, direction='wide', timevar='year', idvar='alti')

每个 .wide 集合都有您想要的数据。唯一的公共列是 alti 所以我们采用 merge 默认值:

 merge(mon.wide, yr.wide)
##   alti meanTemp.aug. maxTemp.aug. meanTemp.sep. maxTemp.sep. meanTemp.2011 maxTemp.2011
## 1  350     10.666667     14.33333            10    11.000000     10.333333    12.666667
## 2  550      8.666667     10.33333             8     9.333333      8.333333     9.833333

这是 data.table 解决方案的另一种变体,但这需要当前的 devel version, v1.9.5:

require(data.table) # v1.9.5+
setDT(clim)
form = paste("alti", c("year", "month"), sep=" ~ ")
val  = c("meanTemp", "maxTemp")
ans  = lapply(form, function(x) dcast(clim, x, mean, value.var = val))
Reduce(function(x, y) x[y, on="alti"], ans)

#    alti meanTemp_mean_2011 maxTemp_mean_2011 meanTemp_mean_aug. meanTemp_mean_sep. maxTemp_mean_aug. maxTemp_mean_sep.
# 1:  350          10.333333         12.666667          10.666667                 10          14.33333         11.000000
# 2:  550           8.333333          9.833333           8.666667                  8          10.33333          9.333333