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

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 解决方案的另一种变体,但这需要当前的 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

关于重塑和均值计算,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31587160/

10-12 23:26