简体   繁体   中英

how to convert date and time interval to fit a time series model in R

I have the following data set and I am trying to create a time series model from the variables.

>Count
    Date        TimeSlot    UserCount
    2013/06/11  6.00-6.10   0
    2013/06/11  6.10-6.20   1
    2013/06/11  6.20-6.30   0
    2013/06/11  6.30-6.40   0
    2013/06/11  6.40-6.50   2
    2013/06/11  6.50-7.00   6

How can I create a time series from the above column variables.?

I am new to time series analysis and I know that given different date values I can create a time series using the 'xts' package as follows.

x <- xts(Count$UserCount,Count$Date)

But given the above data which is unique from both the date and time interval how can this be done?

Since the combination of Date and TimeSlot is unique the only thing you need to do is to create a POSIXct class out of them.

There is no point for the time class to be of the form 6.00-6.10 , 6.10-6.20 . You can only use the first time ie 6.00 , 6.10 etc. and obviously it will be implied that each row represents a 10 minute interval. This is what you do when you work with aggregated timestamps anyway. This is the normal way.

So, something like this will work:

Count$timestamp <- as.POSIXct(paste(Count$Date, substr(Count$TimeSlot,1,4)),
                              format='%Y/%m/%d %H.%M')

#> Count
#        Date  TimeSlot UserCount           timestamp
#1 2013/06/11 6.00-6.10         0 2013-06-11 06:00:00
#2 2013/06/11 6.10-6.20         1 2013-06-11 06:10:00
#3 2013/06/11 6.20-6.30         0 2013-06-11 06:20:00
#4 2013/06/11 6.30-6.40         0 2013-06-11 06:30:00
#5 2013/06/11 6.40-6.50         2 2013-06-11 06:40:00
#6 2013/06/11 6.50-7.00         6 2013-06-11 06:50:00

And then create your timeseries:

library(xts)
x <- xts(Count$UserCount, Count$timestamp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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