简体   繁体   English

在R中以24小时格式创建时间序列数据

[英]create time series data in 24 hours format in r

time_index <- seq(from = as.POSIXct("2012-08-25 00:00"),
              to  = as.POSIXct("2014-09-25 23:00"),
              by = "hour")
ts_data <- xts(x = train$Count,
           order.by = time_index)

Using the above code I've created a time series data. 使用上面的代码,我创建了一个时间序列数据。 This is a traffic data detailing how many vehicles visited a particular signal. 这是交通数据,详细说明有多少辆车访问了特定信号。

I would like to find the traffic patterns in the data. 我想在数据中找到流量模式。 The data contains the hourly count of vehicles each hour. 数据包含每小时的每小时小时数。

Now the question is how to convert this data to have 24 columns with dates in the rows of the time series and hours in the columns? 现在的问题是如何将这些数据转换为24列,其中时间序列的行中包含日期,而时间列中则包含小时。

I'm using R programming to achieve this. 我正在使用R编程来实现这一点。

the following codes might be an useful example to solve your problem. 以下代码可能是解决问题的有用示例。

require(tidyverse)
ts_data<-as.data.frame(ts_data)
ts_data$datetime<-rownames(ts_data)
ts_data<-ts_data %>% separate(datetime,into=c("date","time"),sep=" ") 
ts_data<-ts_data %>% separate(time,into=c("HH","MM","SS")) 
ts_data<-ts_data %>% select(V1,date,HH)
colnames(ts_data)<-c("count","date","HH")
ts_data<-ts_data %>% spread(date,count)

Since I'm not familiar with xts object, I converted it into data.frame. 由于我不熟悉xts对象,因此将其转换为data.frame。

I hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM