How to get the sum of each four rows of a matrix in R

自作多情 提交于 2019-11-30 09:29:25
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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!