如何快速将数据框中的时间列分组为间隔? [英] How do I quickly group the time column in a dataframe into intervals?

查看:117
本文介绍了如何快速将数据框中的时间列分组为间隔?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设在R中,我有一个data.frame,第一列代表时间(如POSIXct)。其余的列(例如,第2列)是数字数据。



我想将时间分成3分钟的时间间隔。每个区间将是落入该特定区间的值的平均值。

现在,我有一个for循环遍历时间列并生成区间苍蝇。我想知道是否有更加优雅的方式来完成同样的事情?



预先致谢。



德里克

解决方案

我认为像下面这样的命令会返回一个3分钟间隔的值列表。 ( v 是数据框的名称, datecol 是日期列的名称)

  library(plyr)

v <-data.frame(datecol = as.POSIXct(c(
2010-01-13 03:02:38 UTC,
2010-01-13 03:03:14 UTC,
2010-01-13 03:05:52 UTC,
2010-01-13 03:07:42 UTC,
2010-01-13 03:09:38 UTC,
2010-01-13 03:10:14 UTC,
2010-01-13 03:12:52 UTC,
2010-01-13 03:13:42 UTC,
2010-01-13 03 :15:42 UTC,
2010-01-13 03:16:38 UTC,
2010-01-13 03:18:14 UTC,
2010- 01-13 03:21:52 UTC,
2010-01-13 03:22:42 UTC,
2010-01-13 03:24:19 UTC,
2010-01-13 03:25:19 UTC
)),x = cumsum(runif(15)* 10),y = cumsum(runif(15)* 20))


dlply(v,。(cut(datecol,3 min)),[)


Assuming in R, I have a data.frame with the first column representing the time (as POSIXct). The rest of the columns (e.g., columns 2) are numeric data.

I would like to group time into 3-minute intervals. Each interval will the the average of values that falls into that particular interval.

Right now, I have a for-loop that iterates through the time column and generate the interval on the fly. I am wondering if there's a more elegant way to accomplish the same thing?

Thanks in advance.

Derek

解决方案

I think that a command like the following, would return a list of the values that fall into 3 minute intervals. (v is the name of the dataframe and datecol is the name of the date column)

library(plyr)

v<-data.frame(datecol=as.POSIXct(c(
  "2010-01-13 03:02:38 UTC",
  "2010-01-13 03:03:14 UTC",
  "2010-01-13 03:05:52 UTC",
  "2010-01-13 03:07:42 UTC",
  "2010-01-13 03:09:38 UTC",
  "2010-01-13 03:10:14 UTC",
  "2010-01-13 03:12:52 UTC",
  "2010-01-13 03:13:42 UTC",
  "2010-01-13 03:15:42 UTC",
  "2010-01-13 03:16:38 UTC",
  "2010-01-13 03:18:14 UTC",
  "2010-01-13 03:21:52 UTC",
  "2010-01-13 03:22:42 UTC",
  "2010-01-13 03:24:19 UTC",
  "2010-01-13 03:25:19 UTC"
)), x = cumsum(runif(15)*10),y=cumsum(runif(15)*20))


dlply(v,.(cut(datecol,"3 min")),"[")

这篇关于如何快速将数据框中的时间列分组为间隔?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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