[R] Sum of some months totals

David L Carlson dcarlson at tamu.edu
Tue Apr 14 20:29:22 CEST 2015


Don't use html formatted emails and always copy the list on your replies.

For example?

rainstats <- function(data, months=3) {
     if (! months %in% c(1, 2, 3, 4, 6, 12)) stop("Months must divide into 12!")
     period <- 12/months
     grps <- rep(1:period, each=months)
     Group <- grps[rainfall$Month]
     aggregate(Rain~Year+Group, rainfall, function(x) c(sum=sum(x),
         days=sum(x>0)))
}

> rainstats(rainfall)
  Year Group Rain.sum Rain.days
1 1979     1        0         0

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352



From: Frederic Ntirenganya [mailto:ntfredo at gmail.com] 
Sent: Tuesday, April 14, 2015 9:27 AM
To: David L Carlson
Subject: Re: [R] Sum of some months totals

Hi David,
I understand what you did. My aim is to make a function which takes a quarter as a default. i.e I can compute lets say for 4 motnhs by specifying it in the arguments of the function.
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 4:44 PM, David L Carlson <dcarlson at tamu.edu> wrote:
You should read some beginning tutorials for R before you go further. You are wasting a lot of time writing complicated loops that you do not need. R is probably very different from the programming languages you are used to. In these examples I called your data "rainfall."

To get the sum of the rain for each month you need only:

aggregate(Rain~Year+Month, rainfall, sum)

To get the number of days with rain is slightly more complicated:

aggregate(Rain~Year+Month, rainfall, function(x) sum(x>0))

To get the sum for a quarter, you need to add quarters to your data frame, eg. Notice that it does not require a loop to add an entire column to your existing data frame.

rainfall$Quarter <- (rainfall$Month+2) %/% 3
aggregate(Rain~Year+Quarter, rainfall, sum)

The command ?aggregate will bring up a manual page on the aggregate() function.

Read
"Introduction to R" at http://cran.r-project.org/manuals.html
and one or more of the contributed manuals at
http://cran.r-project.org/other-docs.html

-------------------------------------
David L Carlson
Department of Anthropology
Texas A&M University
College Station, TX 77840-4352




-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Frederic Ntirenganya
Sent: Tuesday, April 14, 2015 6:10 AM
To: Adams, Jean
Cc: r-help at r-project.org
Subject: Re: [R] 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:

> If you want to calculate the number of days having greater than a certain
> threshold of rain within a range of months, a function like this might
> serve your needs.
>
> raindays <- function(data, monStart=1, monEnd=3, threshold=0.85) {
>   with(data, {
>     selRows <- Month >= monStart & Month <= monEnd & Rain > threshold
>     days <- tapply(selRows, Year, sum)
>     return(days)
>   })
> }
>
> raindays(kitale)
>
> Jean
>
> On Tue, Apr 14, 2015 at 2:46 AM, Frederic Ntirenganya <ntfredo at gmail.com>
> wrote:
>
>> I want to compute monthly summaries from daily data. I want to choose
>> which
>> month to start and how many months to total over.  Default could be to
>> start in January and total over 3 months.  For the number of rain days the
>> default threshold is 0.85mm.
>>
>> I tried to make a function which sum all months not some of months. I will
>> appreciate any help from you guys. Thanks.
>> Here is the data and the code I used.
>>
>> > dput(head(kitale))structure(list(Year = c(1979L, 1979L, 1979L, 1979L,
>> 1979L, 1979L
>>
>> ), Month = c(1L, 1L, 1L, 1L, 1L, 1L), Day = 1:6, Rain = c(0,
>> 0, 0, 0, 0, 0)), .Names = c("Year", "Month", "Day", "Rain"), row.names =
>> c(NA,
>> 6L), class = "data.frame")
>>
>> here is the function:
>>
>> total = function(data, threshold = 0.85){
>>   month_tot=matrix(NA,31,12)
>>   rownames(month_tot)=as.character(1979:2009)
>>
>> 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 1979:2009) {
>>       month_tot[yr-1978,mon]=sum(rain[rain[,1]==yr,2])
>>       raindays[yr-1978,mon]=sum(rain[rain[,1]==yr,2]>threshold)
>>     }
>>   }
>>   month_tot
>> }
>>
>> 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/
>>
>>         [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
>

        [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list