R的每小时时间序列

时间:2017-09-28 15:34:48

标签: r time time-series zoo

我有POSIXct格式的示例时间数据(如下),但无法使用ts()将其转换为每小时时间序列。

> dat
 [1] "2017-09-25 07:34:53 EDT" "2017-09-25 07:56:43 EDT" "2017-09-25 08:33:40 EDT"
 [4] "2017-09-25 08:34:24 EDT" "2017-09-25 08:43:33 EDT" "2017-09-25 09:34:53 EDT"
 [7] "2017-09-25 09:36:07 EDT" "2017-09-25 09:40:50 EDT" "2017-09-25 10:19:12 EDT"
[10] "2017-09-25 10:22:03 EDT" "2017-09-25 10:27:01 EDT" "2017-09-25 10:35:01 EDT"
[13] "2017-09-25 11:09:54 EDT" "2017-09-25 11:17:37 EDT" "2017-09-25 11:18:25 EDT"
[16] "2017-09-25 11:59:50 EDT" "2017-09-25 12:38:06 EDT" "2017-09-25 12:41:41 EDT"
[19] "2017-09-25 13:02:44 EDT" "2017-09-25 13:17:23 EDT" "2017-09-25 13:30:40 EDT"
[22] "2017-09-25 13:42:03 EDT" "2017-09-25 13:56:27 EDT" "2017-09-25 14:23:59 EDT"
[25] "2017-09-25 15:42:51 EDT"

我的目标是随后使用dygraph()显示每小时的时间序列。在下面的示例中,我按日期生成了时间序列,其中包含以下代码:

trips <- as.data.frame(matrix(ncol=1, nrow=30))
colnames(trips) <- c("count")
trips$count <-  floor(runif(30, min=50, max=101))
z <- zoo(trips, seq(from = as.Date("2017-09-01"), to = as.Date("2017-09-30"), by = 1))

z <- as.ts(z)

dygraph(z, main = "Daily Trips") 

enter image description here

我如何才能正确地将POSIXct转换为每小时的时间序列,知道第一个和最终记录之间的小时数差异?

以下是示例数据:

> dput(dat)
structure(c(1506339293, 1506340603, 1506342820, 1506342864, 1506343413, 
1506346493, 1506346567, 1506346850, 1506349152, 1506349323, 1506349621, 
1506350101, 1506352194, 1506352657, 1506352705, 1506355190, 1506357486, 
1506357701, 1506358964, 1506359843, 1506360640, 1506361323, 1506362187, 
1506363839, 1506368571), class = c("POSIXct", "POSIXt"), tzone = "")

1 个答案:

答案 0 :(得分:1)

也许是这样的:

z = ts(dat, start =1, end =8, frequency = 1)
class(z) <- c("POSIXct", "POSIXt")
#[1] "2017-09-25 13:34:53 GMT" "2017-09-25 13:56:43 GMT" "2017-09-25 14:33:40 GMT" "2017-09-25 14:34:24 GMT" "2017-09-25 14:43:33 GMT" "2017-09-25 15:34:53 GMT" "2017-09-25 15:36:07 GMT" "2017-09-25 15:40:50 GMT"

另一次尝试:

seq(from =dat[1], to = dat[length(dat)], by ='hour')
#[1] "2017-09-25 13:34:53 GMT" "2017-09-25 14:34:53 GMT" "2017-09-25 15:34:53 GMT" "2017-09-25 16:34:53 GMT" "2017-09-25 17:34:53 GMT" "2017-09-25 18:34:53 GMT" "2017-09-25 19:34:53 GMT" "2017-09-25 20:34:53 GMT" "2017-09-25 21:34:53 GMT"

第二个长度为9,原始dat

每个唯一小时一个

第三个选项:

library(lubridate)
library(data.table)

df = data.frame(timestamps = dat, bases = floor_date(dat, 'hour'))

setDT(df)
graph = df[,.N, by = bases]
dygraph(graph)

这将给出以下的dygraph: enter image description here