[R] monthly sum

Rui Barradas ruipbarradas at sapo.pt
Mon Mar 31 19:48:08 CEST 2014


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