I get why vectorised functions are better than for-loops.
But there are some problems where I can’t see the vectorised functional programming solution. One of those is summing monthly data to get quarterly data. Any suggestions to replace this code …
month <- 1:100
A422072L <- c(rep(NA, 4), rnorm(96, 100, 5) ) + 2 * month
A422070J <- c(NA, NA, rnorm(96, 100, 5), NA, NA) + 2 * month
Au.approvals <- data.frame(month=month, A422072L=A422072L, A422070J=A422070J)
Au.approvals$trend.sum.A422072L.qtr <- NA
Au.approvals$sa.sum.A422070J.qtr <- NA
for(i in seq_len(nrow(Au.approvals)))
{
if(i < 3) next
if(all(!is.na(Au.approvals$A422072L[(i-2):i])))
Au.approvals$trend.sum.A422072L.qtr[i] <- sum(Au.approvals$A422072L[(i-2):i])
if(all(!is.na(Au.approvals$A422070J[(i-2):i])))
Au.approvals$sa.sum.A422070J.qtr[i] <- sum(Au.approvals$A422070J[(i-2):i])
}
print(Au.approvals)
Now with enough data to run as an example.
Let’s create some bogus timeseries:
To get a rolling sum, please take a look at
rollapplyfrom the zoo package:here I assume that the coarser resolution (quarterly) is 10 times coarser than the finer resolution.
Original answer for a non-rolling mean:
I like to use the functions from the
plyrpackage, butave,aggregate, anddata.tableare also good options. For large datasets,data.tableis veeery fast. But to get back to someplyrmagic:First create an additional column which specifies the more coarse time frequency, i.e. which quarter is your observation in:
Now we can aggregate
time_datfor the coarser time frequency: