ddply的mean,max,min函数

时间:2013-09-13 09:42:02

标签: r plyr

我有一个半小时值的大型气象数据集。我正在使用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))

1 个答案:

答案 0 :(得分: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-tranformation-styles/是相关的(显然summarisemutate - 风格评估)