在R中的分类日期 [英] Binning Dates in R

查看:164
本文介绍了在R中的分类日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



这是我的数据集:

 > str(temp.df)
'data.frame':74602 obs。的2个变量:
$ time:POSIXct,格式:2011-04-09 03:53:202011-04-09 03:53:152011-04-09 03:53:07 2011-04-09 03:52:39...
$ value:num 1 1 1 1 1 1 1 1 1 1 ...

>头(temp.df $ time,n = 10)
[1]2011-04-09 03:53:20 EDT2011-04-09 03:53:15 EDT2011-04- 09 03:53:07 EDT2011-04-09 03:52:39 EDT
[5]2011-04-09 03:52:29 EDT2011-04-09 03:51 :56 EDT2011-04-09 03:51:54 EDT2011-04-09 03:51:46 EDT
[9]2011-04-09 03:51:44 EDT 2011-04-09 03:51:26 EDT

为了方便...



 > dput(head(temp.df $ time,n = 10))
结构(c(1302335600,1302335595,1302335587,1302335559,130​​2335549,
1302335516,1302335514,1302335506,1302335504,1302335486),class = c(POSIXct,
POSIXt),tzone =)

我正在寻找:




  • 如何找到最小和最大日期/时间之间的时间?

  • 使用1小时时间段创建数据摘要的最佳方式是什么?



任何帮助,您可以提供将不胜感激

解决方案

使用正确的时间序列包动物园和/或 xts 。这个例子是直接从 aggregate.zoo()的帮助页面,它每10分钟聚合POSIXct秒数据

  tt < -  seq(10,2000,10)
x< - zoo(tt,structure(tt,class = c(POSIXt ,POSIXct)))
aggregate(x,time(x) - as.numeric(time(x))%% 600,mean)

to.period()函数。 org / package = xtsrel =nofollow noreferrer> xts 也是一个肯定的赢家。在这里有关于SO和r-sig财务列表的无数例子。


I struggle with dates and times in R, but I am hoping this is a fairly basic task.

Here is my dataset:

> str(temp.df)
'data.frame':   74602 obs. of  2 variables:
 $ time : POSIXct, format: "2011-04-09 03:53:20" "2011-04-09 03:53:15" "2011-04-09 03:53:07" "2011-04-09 03:52:39" ...
 $ value: num  1 1 1 1 1 1 1 1 1 1 ...

> head(temp.df$time, n=10)
 [1] "2011-04-09 03:53:20 EDT" "2011-04-09 03:53:15 EDT" "2011-04-09 03:53:07 EDT" "2011-04-09 03:52:39 EDT"
 [5] "2011-04-09 03:52:29 EDT" "2011-04-09 03:51:56 EDT" "2011-04-09 03:51:54 EDT" "2011-04-09 03:51:46 EDT"
 [9] "2011-04-09 03:51:44 EDT" "2011-04-09 03:51:26 EDT"

and for convenience...

> dput(head(temp.df$time, n=10))
structure(c(1302335600, 1302335595, 1302335587, 1302335559, 1302335549, 
1302335516, 1302335514, 1302335506, 1302335504, 1302335486), class = c("POSIXct", 
"POSIXt"), tzone = "")

What I am looking to do:

  • How can I find how many hours are between the min and max date/time?
  • What's the best way to create summaries of my data using 1-hour time buckets?

Any help you can provide will be greatly appreciated

解决方案

Use the proper time series packages zoo and/or xts. This example is straight from the help pages of aggregate.zoo() which aggregates POSIXct seconds data every 10 minutes

 tt <- seq(10, 2000, 10)
 x <- zoo(tt, structure(tt, class = c("POSIXt", "POSIXct")))
 aggregate(x, time(x) - as.numeric(time(x)) %% 600, mean)

The to.period() function in xts is also a sure winner. There are countless examples here on SO and on the r-sig-finance list.

这篇关于在R中的分类日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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