1

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?

Tung
  • 26,371
  • 7
  • 91
  • 115
Davido
  • 121
  • 4
  • 12

1 Answers1

2
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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
GKi
  • 37,245
  • 2
  • 26
  • 48
  • thanks but none of this is my case, I have a daily time series for 79 years, i expect to obtain from it a vector of 12 values (one for each month), this only value for each month should represent the average of all the monthly-sum values of each year. please let me know if I do explain myself clear – Davido Apr 09 '19 at 12:09
  • If the number of observations are the same e.g. for January in each of your years the result should be the same. With mean you calculate the mean precipitation per day. If you multiply this with the number of days in the month you get the mean precipitation per month what is equal to the average of all the monthly-sum values. – GKi Apr 09 '19 at 12:31
  • NO. when I say the sum I mean the accumulated value (the sum of the values from the day 01 till the day 30 of each month). This value would be different for the next year, and for the next year and so on, I need an average of this accumulated values for each month considering all years. – Davido Apr 09 '19 at 12:42
  • at the end I should get an average value for each month. I hope its clear my explanation – Davido Apr 09 '19 at 12:49
  • Do you simply need the result of the average monthly precipitation or is there an urgent need to follow exactly the way you expect how it is calculated? Please have a look at "Add" in my answer which shows that there is more than one way to come to the desired result. The only condition to the other ways is that your data also includes observation of 0 precipitation, what is typically the case for precipitation observations. – GKi Apr 09 '19 at 13:16
  • I dont need an exact method but thinking about it I think I can make it work with one of your suggestions. Thanks – Davido Apr 09 '19 at 13:43