Reputation: 121
I have a long time series (zoo) of precipitation data, I know how to obtain the monthly average of the values:
library(hydroTSM)
ma= monthlyfunction(data, mean, na.rm=TRUE)
I also know how to obtain the monthly sum of the values:
su= monthlyfunction(data, sum, na.rm=TRUE)
but with the last one I get a monthly sum for the whole period of the time serie. I would like to get a monthly average of the sums, I mean for example:
jan 1980 (sum)= 150
jan 1981 (sum)= 180
jan 1982 (sum)= 90
expected value for january = average(150,180,90)= 140
Is there a function for this instead of mean and sum?
Upvotes: 1
Views: 1087
Reputation: 39717
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
Upvotes: 2