Sabra Ossen
Sabra Ossen

Reputation: 161

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?

Upvotes: 1

Views: 514

Answers (1)

LyzandeR
LyzandeR

Reputation: 37889

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 i.e. 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)

Upvotes: 3

Related Questions