相关疑难解决方法(0)

什么是从数据帧中的带时间戳的行分区和聚合间隔的有效方法?

从带有时间戳行的数据框(strptime结果),聚合间隔统计数据的最佳方法是什么?

间隔可以是一小时,一天等.

aggregate功能,但这对于将每一行分配给一个间隔没有帮助.我打算在数据框中添加一个表示间隔的列并使用它aggregate,但是如果有更好的解决方案,那么听到它会很棒.

感谢您的任何指示!


示例数据

五行,时间戳从03:00开始分为15分钟.

间隔1

  • "2010-01-13 03:02:38 UTC"
  • "2010-01-13 03:08:14 UTC"
  • "2010-01-13 03:14:52 UTC"

间隔2

  • "2010-01-13 03:20:42 UTC"
  • "2010-01-13 03:22:19 UTC"

结论

使用时间序列包xts应该是解决方案; 但是我没有成功使用它们并且使用它们cut.由于我目前只需绘制直方图,行按间隔分组,这就足够了.

cut 使用喜欢如此:

interv <- function(x, start, period, num.intervals) {
  return(cut(x, as.POSIXlt(start)+0:num.intervals*period))
}
Run Code Online (Sandbox Code Playgroud)

r xts

5
推荐指数
2
解决办法
2491
查看次数

如何根据R中的时间间隔对数据进行分组

我的数据看起来像这样:

library(plyr)
dates<-data.frame(datecol=as.POSIXct(c(
  "2010-04-03 03:02:38 UTC",
  "2010-04-03 03:03:14 UTC",
  "2010-04-20 03:05:52 UTC",
  "2010-04-20 03:07:42 UTC",
  "2010-04-21 03:09:38 UTC",
  "2010-04-21 03:10:14 UTC",
  "2010-04-21 03:12:52 UTC",
  "2010-04-23 03:13:42 UTC",
  "2010-04-23 03:15:42 UTC",
  "2010-04-23 03:16:38 UTC",
  "2010-04-23 03:18:14 UTC",
  "2010-04-24 03:21:52 UTC",
  "2010-04-24 03:22:42 UTC",
  "2010-04-24 03:24:19 UTC",
  "2010-04-24 03:25:19 UTC"
)), x = cumsum(runif(15)*10),y=cumsum(runif(15)*20))
Run Code Online (Sandbox Code Playgroud)

我想将我的数据分组为5天,因此所有5天或更短时间的点都放在一个组中.我尝试了这里建议的内容:

gr<-ddply(dates,.(cut(datecol,"5 day",include.lowest = TRUE)),"[")
Run Code Online (Sandbox Code Playgroud)

但由于某些原因,我最终得到3组而不是2组,而04/21和04/23的分数分成不同的组,即使它们相隔不到5天.

这是我想得到的:

         group             datecol         x          y
1            1 2010-04-03 03:02:38  8.112423   4.790036
2            1 2010-04-03 03:03:14 11.184709  22.903475
3            2 2010-04-20 …
Run Code Online (Sandbox Code Playgroud)

4
推荐指数
1
解决办法
3811
查看次数

标签 统计

r ×1

xts ×1