0
Time  Customer Count
11:00 13
13:00 25
15:00 22
17:00 21
19:00 15
21:00 10

I have the above data frame for the number of customers coming into a small shop from 11:00 (11am) to 21:00 (9pm). I need to make a time series of this data but I'm having trouble declaring the ts function for this data. There is only data for 10 hours from 11 to 9 and it's only taken every two hours. I can't decide how to declare the frequency.

If anyone could help, would be really grateful. Thanks in advance

JohnBanter
  • 49
  • 3
  • Possible duplicated [How to Create a R TimeSeries for Hourly data](https://stackoverflow.com/q/17156143/1315767) – Jilber Urbina Apr 12 '18 at 23:07
  • 1
    Not duplicated mate. I've been told it's possible with the standard ts() function – JohnBanter Apr 12 '18 at 23:14
  • @JohnBanter take a look at the links in the comment section to that answer. Worth a read for background if not exactly what you are looking for. – lmo Apr 12 '18 at 23:20

1 Answers1

0

One option is to create sequence 2 hours basis and then filter for time-range as:

library(lubridate)
v <- seq(from = as.POSIXct("2017-01-01 11:00"), 
    to = as.POSIXct("2017-01-05 21:00"), by = "2 hour")

v[hour(v)>=9 & hour(v)<=21]
# [1] "2017-01-01 11:00:00 GMT" "2017-01-01 13:00:00 GMT" "2017-01-01 15:00:00 GMT"
# [4] "2017-01-01 17:00:00 GMT" "2017-01-01 19:00:00 GMT" "2017-01-01 21:00:00 GMT"
# [7] "2017-01-02 09:00:00 GMT" "2017-01-02 11:00:00 GMT" "2017-01-02 13:00:00 GMT"
# [10] "2017-01-02 15:00:00 GMT" "2017-01-02 17:00:00 GMT" "2017-01-02 19:00:00 GMT"
# [13] "2017-01-02 21:00:00 GMT" "2017-01-03 09:00:00 GMT" "2017-01-03 11:00:00 GMT"
# [16] "2017-01-03 13:00:00 GMT" "2017-01-03 15:00:00 GMT" "2017-01-03 17:00:00 GMT"
# so on few more rows.

The above seq generates time-series from 1st January till 5th January at 2 hours interval. A filter condition is applied once series has been generated. The filter condition considers only time-date for which hour >= 9 and hour <= 21. This will provide desired time-series.

MKR
  • 19,739
  • 4
  • 23
  • 33