R-时间序列每月总和的平均值

时间:2019-04-09 09:36:44

标签: r sum time-series mean zoo

我有一个很长的降雨数据序列(动物园),我知道如何获取这些值的每月平均值:

COPY ./ClientApp/package.json /app/package.json

我也知道如何获取这些值的每月总和:

library(hydroTSM)
ma= monthlyfunction(data, mean, na.rm=TRUE)

但是最后一个,我得到了整个时间序列的每月总和。我想获得每月平均金额,例如:

su= monthlyfunction(data, sum, na.rm=TRUE)

是否有此功能而不是均值和和?

1 个答案:

答案 0 :(得分:1)

library(hydroTSM)

#This data is daily streamflows, but is similar to Precipitation
data(OcaEnOnaQts)
x <- OcaEnOnaQts

#In case you want monthly precipitation in "precipitation / 30 days" (what is common) you can use
monthlyfunction(x, FUN=mean, na.rm=TRUE) * 30

#In case you want the precipitation per days in specific month you can use
monthlyfunction(x, FUN=mean, na.rm=TRUE) * as.vector(dwi(x, out.unit = "months") * mean(dwi(x)) / sum(dwi(x)))

#or approximately
monthlyfunction(x, FUN=mean, na.rm=TRUE)*c(31,28.25,31,30,31,30,31,31,30,31,30,31)


#Add: Some ways to come to the mean monthly precipitation
p1980 <- c(rep(0,28), 50, 50, 50) #sum = 150
p1981 <- c(rep(0,28), 60, 60, 60) #sum = 180
p1982 <- c(rep(0,28), 30, 30, 30) #sum = 90
#
mean(c(sum(p1980), sum(p1981), sum(p1982))) # = 140 This is how you want it to be calculated
mean(c(p1980, p1981, p1982))*31 # = 140 This is how I suggested to come to the result
#Some other ways to come to the mean monthly precipitation
mean(c(mean(p1980), mean(p1981), mean(p1982)))*31 # = 140
sum(c(p1980, p1981, p1982))/3 # = 140