Skip to content
Prev 349963 / 398513 Next

Sum of some months totals

Hi Jean,

Thanks for the help!
How can I compute monthly total of rainfall?

I want to compute both monthly total of rainfall and number of raindays. In
below function month_tot is a table and I want to some month. default is 3
months. The loop for quarter is not working and I am wondering why it is
not working.

total = function(data, threshold = 0.85){
  month_tot=matrix(NA,length(unique(data$Year)),12)
  rownames(month_tot)=as.character(unique(data$Year))

colnames(month_tot)=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
  raindays=month_tot
  # loop over months and years to get summary statistics
  for (mon in 1:12) {
    rain=data[data[2]==mon,c(1,4)]   # rain just for a specific month
    for (yr in unique(data$Year)) {
      month_tot[yr-min(unique(data$Year)-1),mon]=sum(rain[rain[,1]==yr,2])
      #print(sum(rain[rain[,1]==yr,2]))

raindays[yr-min(unique(data$Year)-1),mon]=sum(rain[rain[,1]==yr,2]>threshold)
    }
  }
  month_tot
  1:ncol(month_tot)
  #month_tot[,1] + month_tot[,2] + month_tot[,3]
  quarter <-c()
  i = 3
  for (i in 1:ncol(month_tot)){

    quarter[i] = sum(month_tot[,i])
  }
  quarter
}

total(kitale)

Regards,
Frederic.

Frederic Ntirenganya
Maseno University,
African Maths Initiative,
Kenya.
Mobile:(+254)718492836
Email: fredo at aims.ac.za
https://sites.google.com/a/aims.ac.za/fredo/
On Tue, Apr 14, 2015 at 1:52 PM, Adams, Jean <jvadams at usgs.gov> wrote: