首页 > 解决方案 > R - 时间序列每月总和的平均值

问题描述

我有很长的降水数据时间序列(动物园),我知道如何获得这些值的月平均值:

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

我也知道如何获得值的每月总和:

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

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

jan 1980 (sum)= 150
jan 1981 (sum)= 180
jan 1982 (sum)= 90

expected value for january = average(150,180,90)= 140

有没有一个函数来代替平均值和总和?

标签: rtime-seriesmeanzoohydrotsm

解决方案


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

推荐阅读