I am wondering if there is a way to average daily data into weekly data. The dataframe that I call CADaily looks like this:
> CADaily[1:10, ]
Climate_Division Date Rain
885 1 1948-07-01 0.8750000
892 1 1948-07-02 2.9166667
894 1 1948-07-03 0.7916667
895 1 1948-07-04 0.4305556
898 1 1948-07-05 0.8262061
901 1 1948-07-06 0.5972222
904 1 1948-07-17 0.04166667
905 1 1948-07-18 0.08333333
907 1 1948-07-20 0.04166667
909 1 1948-07-22 0.12500000
910 1 1948-07-21 NA
My objective is similar to the aggregate function to find the average of the daily rain into weekly rain values base on the Date (of course) and the Climate_Division (ranges from 1 to 7). I was searching online and I came across a code that I was able to use but wasn't quite to par to my objective:
apply.weekly(xts(CADaily[,-2], order.by= CADaily[,2]), FUN = mean)
This does what I would like it to do, however my column Climate_Division is also averaged. I would simply like to average Rain only and order it based on the Climate_Division followed by Date. Is there a way that I can possibly do it as follows:
aggregate(CADaily, by =list(CADaily$Climate_Division, CADaily$Date), FUN = mean, na.rm = TRUE)
where Date is in some form of weeks? Or is there another way?
EDIT:
Dear All,
Thank you for your help. Perhaps using aggregate wasn't the best way to go about this as I originally thought. In terms of output, I wanted to obtain the weekly average of rain throughout the years of the data (1948 - 1995). In other words, I wanted to get a nice format that I can input into a time series that has the form of the date of the end of the week. The output that I am looking for (keeping in mind that there may exist NA values) is:
Climate_Division Date Rain
1 1948-07-03 1.527778
1 1948-07-10 0.6179946
1 1948-07-17 0.04166667
1 1948-07-24 0.08333333
...
1 1995-12-23 0.24513245
1 1995-12-30 0.12450545
Or is there a better way of expressing weekly data that is represented by dates?
Thank you for your help.
See Question&Answers more detail:os