[R] monthly sum

Rui Barradas ruipbarradas at sapo.pt
Mon Mar 31 21:04:37 CEST 2014


Hello,

Sorry, but I have no idea why the error message, with me it works 
correctly. Are you sure that the four columns A, B, C and D are numeric?
What does str(dat) say?

Rui Barradas

Em 31-03-2014 19:40, eliza botto escreveu:
> Dear Rui,
>
> Thanks for your reply. But the command seems not to be working. I am
> getting the following error.
>
> Error in FUN(X[[1L]], ...) : invalid 'type' (character) of argument
>
> Any idea? :(
>
> Thanks,
>
> Eliza
>
>
>  > Date: Mon, 31 Mar 2014 18:48:08 +0100
>  > From: ruipbarradas at sapo.pt
>  > To: eliza_botto at hotmail.com; r-help at r-project.org
>  > Subject: Re: [R] monthly sum
>  >
>  > Hello,
>  >
>  > Maybe the following will do.
>  >
>  >
>  > library(zoo)
>  > ym <- as.yearmon(dat$Date, "%d-%B-%y")
>  > aggregate(dat[,-1], list(ym), FUN = sum, na.rm = TRUE)
>  >
>  >
>  > Also, please use dput() to post data examples.
>  >
>  > Hope this helps,
>  >
>  > Rui Barradas
>  >
>  > Em 31-03-2014 18:31, eliza botto escreveu:
>  > > Dear useRs,
>  > > I have the data of following format. I have only pasted some part
> of the data. The data starts from 1961 and ends up in december 1987.
>  > >
>  > > dat <- read.table(text="Date A B C D1-Jan-61 0.00 1.27 8.128
> 0.252-Jan-61 6.10 9.144 94.742 15.493-Jan-61 0.00 0.508 1.27
> 0.004-Jan-61 0.00 0 NA 0.005-Jan-61 0.00 0 0 0.006-Jan-61 0.00 NA 0
> 0.007-Jan-61 0.00 0 0 0.008-Jan-61 0.00 NA 0 0.009-Jan-61 0.00 NA 0
> NA10-Jan-61 0.00 4.064 4.826
> 0.76",sep="",header=TRUE,stringsAsFactors=FALSE)
>  > >
>  > > I need to calculate the sum of each months of A,B,C and D. There
> are NA's in each column so whenever there is NA it should be ignored.
>  > > In the end we should have a table with 12 values for each year or
> all the for columns having dimension (12*number of years rows and 4
> columns).
>  > >
>  > > Thankyou very much in advance
>  > > Eliza
>  > > [[alternative HTML version deleted]]
>  > >
>  > > ______________________________________________
>  > > R-help at r-project.org mailing list
>  > > 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