如何获得R中矩阵的每四行的总和 [英] How to get the sum of each four rows of a matrix in R

查看:13
本文介绍了如何获得R中矩阵的每四行的总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 4n x m 矩阵(一年中以 7.5 分钟间隔求和).我想将这些转换为 30 分钟的总和,例如将 70080 x 1 转换为 17520 矩阵.

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 版本:2.13;平台:x86_64-pc-linux-gnu(64位)

R version: 2.13; Platform: x86_64-pc-linux-gnu (64-bit)

推荐答案

colSums(matrix(test.data$observation, nrow=4))

这篇关于如何获得R中矩阵的每四行的总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆