在R中以等间隔的时间间隔聚合数据帧 [英] Aggregate a data frame in R by equally spaced time intervals

查看:136
本文介绍了在R中以等间隔的时间间隔聚合数据帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想按时间汇总数据,并创建相等间隔的时间间隔:

I want to aggregate the data by time and create equally spaced time intervals:

date<- c(as.POSIXct("2011-08-08 21:00:00"), as.POSIXct("2011-08-08 21:26:00"))
value<-c(1,2)
dt<-data.frame(date, value)

DT<-aggregate(cbind(dt$value),list(cut(dt$date, breaks="10 min")),sum) 

dt:
2011-08-08 21:00:00 1
2011-08-08 21:26:00 2

DT:
2011-08-08 21:00:00 1
2011-08-08 21:20:00 2

我想要的:

2011-08-08 21:00:00 1
2011-08-08 21:10:00 NA
2011-08-08 21:20:00 2

有没有使用动物园或xts吗?

Is there anyway to do this without using zoo or xts?

推荐答案

我假设你的最后一行你试图避免使用软件包。

I'm assuming by your last line that you are trying to avoid using packages.

使用基础R进行粘贴,您可以尝试点击,但是你的日期将成为 rownames (或 names ,如果你sk ip data.frame step):

Sticking with base R, you can try tapply, but your dates will become the rownames (or names, if you skip the data.frame step):

data.frame(value = tapply(cbind(dt$value),
                          list(cut(dt$date, breaks="10 min")),
                          sum))
#                     value
# 2011-08-08 21:00:00     1
# 2011-08-08 21:10:00    NA
# 2011-08-08 21:20:00     2

这篇关于在R中以等间隔的时间间隔聚合数据帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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