I have a 4n by m matrix (sums at 7.5 min intervals for a year). I would like to transform these to 30 min sums, e.g. convert a 70080 x 1 to a 17520 matrix.
What is the most computationally efficient way to do this?
More specifics: here is an example (shortened to one day instead of one year)
library(lubridate)
start.date <- ymd_hms("2009-01-01 00:00:00")
n.seconds <- 192 # one day in seconds
time <- start.date + c(seq(n.seconds) - 1) * seconds(450)
test.data <- data.frame(time = time,
observation = sin(1:n.seconds / n.seconds * pi))
R version: 2.13; Platform: x86_64-pc-linux-gnu (64-bit)
colSums(matrix(test.data$observation, nrow=4))
I'm going to make a set of crazy assumptions since your question is fairly ambiguous.
I'll assume your data is a matrix with observations every 7.5 min and there is NO spatial index. So 100 rows might look like this:
data <- matrix(rnorm(400), ncol=4)
and you want to sum chunks of 4 rows.
There's a bunch of ways to do this, but the first one to hop in my head is to create an index and then do the R version of a "group by" and sum.
An example index could be something like this:
index <- rep(1:25, 4)
index <- index[order(index)]
So now that we have an index of the same length as the data, you can use aggregate()
to sum things up:
aggregate(x=data, by = list(index), FUN=sum)
EDIT:
The spirit of the above method may still work. However if you do much work with timeseries data you should probably get to know the xts package. Here's an xts example:
require(xts)
test.xts <- xts(test.data$observation, order.by=test.data$time)
period.apply(test.xts, endpoints(test.xts,"minutes", 30), sum)
sapply(split(test.data$observation, rep(1:(192/4), each=4)), sum)
来源:https://stackoverflow.com/questions/6874203/how-to-get-the-sum-of-each-four-rows-of-a-matrix-in-r