0

我有一个带有半小时值的大型气象数据集。我正在使用 ddply 函数来创建每日值(平均值、总和等),但是我在获取最小值和最大值时遇到了问题!对于某些值,例如气温,我想提取每日平均值、最小值和最大值,但是返回的数据框给我的最小值和最大值与平均值相同。有人可以看看我写的命令,看看他们是否发现任何错误?

HFTda <- ddply (HFT, .(Year, DOY),summarise, 
                Temp = mean (Temp, na.rm = F), 
                Tmax = max (Temp, na.rm = F),
                Tmin = min (Temp, na.rm = F),
                RHmax = max (RH.x, na.rm = F), 
                RHmin = min (RH.x, na.rm = F),
                VPD = mean (VPD.x, na.rm = F), 
                Grad = mean (GradInc2, na.rm = F), 
                Gex = mean (Gex, na.rm = F),
                Prec = sum (Prec, na.rm = F), 
                H = mean (HFill, na.rm = F), 
                LE = mean (LEFill, na.rm = F), 
                NEE = mean (co2_fluxFill, na.rm = F), 
                ET = mean (h2o_fluxFill, na.rm = F), 
                SWout = mean (YB5214h, na.rm = F))
4

1 回答 1

4

尽管缺乏可重复的示例,但我认为我有一个想法;我认为问题在于,temp在尝试获取最小值和最大值之前,您正在取平均值(并在本地覆盖值):

预赛:

d <- data.frame(day=rep(1:2,each=4),Temp=1:8)
library(plyr)

精简示例:

ddply(d, .(day), summarise,
   Temp = mean (Temp),
   minTemp = min(Temp))
 ##   day Temp minTemp
 ## 1   1  2.5     2.5
 ## 2   2  6.5     6.5

最简单的解决方法就是更改顺序:

 ddply(d, .(day), summarise,
   minTemp = min(Temp),
   Temp = mean (Temp))
##   day minTemp Temp
## 1   1       1  2.5
## 2   2       5  6.5

或者(也许更好)您可以将您的平均变量称为meanTemp...

http://r4stats.com/2013/01/22/comparing-transformation-styles/是相关的(显然summarisemutate-style评估)

于 2013-09-13T11:11:38.540 回答