开发者

Binning Dates in R

开发者 https://www.devze.com 2023-02-23 11:29 出处:网络
I struggle with dates and times in R, but I am hoping this is a fairly basic task. Here is my dataset:

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, 1302335开发者_StackOverflow中文版486), 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消