Создайте нерегулярную последовательность с помощью lubridate в R

#r #time-series #xts #lubridate

#r #временные ряды #xts #lubridate

Вопрос:

Я пытаюсь создать временной ряд в R с помощью xts(). Мой набор данных детализирует спрос пассажиров каждые 15 минут с 06:30 до 22:00 с 2005-03-01 по 2005-03-21. С lubridate следующий код приближает меня, но также включает нежелательные итерации с 22:15 до 06:15 (выходные данные обрезаны для пробела).

 dates = seq(ymd_hms('2015-03-01 06:30:00'), ymd_hms('2015-03-21 22:00:00'), by = '15 mins')
   [1] "2015-03-01 06:30:00 UTC" "2015-03-01 06:45:00 UTC" "2015-03-01 07:00:00 UTC"
   [4] "2015-03-01 07:15:00 UTC" "2015-03-01 07:30:00 UTC" "2015-03-01 07:45:00 UTC"
   [7] "2015-03-01 08:00:00 UTC" "2015-03-01 08:15:00 UTC" "2015-03-01 08:30:00 UTC"
  [10] "2015-03-01 08:45:00 UTC" "2015-03-01 09:00:00 UTC" "2015-03-01 09:15:00 UTC"
  [13] "2015-03-01 09:30:00 UTC" "2015-03-01 09:45:00 UTC" "2015-03-01 10:00:00 UTC"
  [16] "2015-03-01 10:15:00 UTC" "2015-03-01 10:30:00 UTC" "2015-03-01 10:45:00 UTC"
  [19] "2015-03-01 11:00:00 UTC" "2015-03-01 11:15:00 UTC" "2015-03-01 11:30:00 UTC"
  [22] "2015-03-01 11:45:00 UTC" "2015-03-01 12:00:00 UTC" "2015-03-01 12:15:00 UTC"
  [25] "2015-03-01 12:30:00 UTC" "2015-03-01 12:45:00 UTC" "2015-03-01 13:00:00 UTC"
  [28] "2015-03-01 13:15:00 UTC" "2015-03-01 13:30:00 UTC" "2015-03-01 13:45:00 UTC"
  [31] "2015-03-01 14:00:00 UTC" "2015-03-01 14:15:00 UTC" "2015-03-01 14:30:00 UTC"
  [34] "2015-03-01 14:45:00 UTC" "2015-03-01 15:00:00 UTC" "2015-03-01 15:15:00 UTC"
  [37] "2015-03-01 15:30:00 UTC" "2015-03-01 15:45:00 UTC" "2015-03-01 16:00:00 UTC"
  [40] "2015-03-01 16:15:00 UTC" "2015-03-01 16:30:00 UTC" "2015-03-01 16:45:00 UTC"
  [43] "2015-03-01 17:00:00 UTC" "2015-03-01 17:15:00 UTC" "2015-03-01 17:30:00 UTC"
  [46] "2015-03-01 17:45:00 UTC" "2015-03-01 18:00:00 UTC" "2015-03-01 18:15:00 UTC"
  [49] "2015-03-01 18:30:00 UTC" "2015-03-01 18:45:00 UTC" "2015-03-01 19:00:00 UTC"
  [52] "2015-03-01 19:15:00 UTC" "2015-03-01 19:30:00 UTC" "2015-03-01 19:45:00 UTC"
  [55] "2015-03-01 20:00:00 UTC" "2015-03-01 20:15:00 UTC" "2015-03-01 20:30:00 UTC"
  [58] "2015-03-01 20:45:00 UTC" "2015-03-01 21:00:00 UTC" "2015-03-01 21:15:00 UTC"
  [61] "2015-03-01 21:30:00 UTC" "2015-03-01 21:45:00 UTC" "2015-03-01 22:00:00 UTC"
  [64] "2015-03-01 22:15:00 UTC" "2015-03-01 22:30:00 UTC" "2015-03-01 22:45:00 UTC"
  [67] "2015-03-01 23:00:00 UTC" "2015-03-01 23:15:00 UTC" "2015-03-01 23:30:00 UTC"
  [70] "2015-03-01 23:45:00 UTC" "2015-03-02 00:00:00 UTC" "2015-03-02 00:15:00 UTC"
  [73] "2015-03-02 00:30:00 UTC" "2015-03-02 00:45:00 UTC" "2015-03-02 01:00:00 UTC"
  [76] "2015-03-02 01:15:00 UTC" "2015-03-02 01:30:00 UTC" "2015-03-02 01:45:00 UTC"
  [79] "2015-03-02 02:00:00 UTC" "2015-03-02 02:15:00 UTC" "2015-03-02 02:30:00 UTC"
  [82] "2015-03-02 02:45:00 UTC" "2015-03-02 03:00:00 UTC" "2015-03-02 03:15:00 UTC"
  [85] "2015-03-02 03:30:00 UTC" "2015-03-02 03:45:00 UTC" "2015-03-02 04:00:00 UTC"
  [88] "2015-03-02 04:15:00 UTC" "2015-03-02 04:30:00 UTC" "2015-03-02 04:45:00 UTC"
  [91] "2015-03-02 05:00:00 UTC" "2015-03-02 05:15:00 UTC" "2015-03-02 05:30:00 UTC"
  [94] "2015-03-02 05:45:00 UTC" "2015-03-02 06:00:00 UTC" "2015-03-02 06:15:00 UTC"
  [97] "2015-03-02 06:30:00 UTC" "2015-03-02 06:45:00 UTC" "2015-03-02 07:00:00 UTC"
 [100] "2015-03-02 07:15:00 UTC" "2015-03-02 07:30:00 UTC" "2015-03-02 07:45:00 UTC"
 [103] "2015-03-02 08:00:00 UTC" "2015-03-02 08:15:00 UTC" "2015-03-02 08:30:00 UTC"
 [106] "2015-03-02 08:45:00 UTC" "2015-03-02 09:00:00 UTC" "2015-03-02 09:15:00 UTC"
 [109] "2015-03-02 09:30:00 UTC" "2015-03-02 09:45:00 UTC" "2015-03-02 10:00:00 UTC"
 [112] "2015-03-02 10:15:00 UTC" "2015-03-02 10:30:00 UTC" "2015-03-02 10:45:00 UTC"
 [115] "2015-03-02 11:00:00 UTC" "2015-03-02 11:15:00 UTC" "2015-03-02 11:30:00 UTC"
 [118] "2015-03-02 11:45:00 UTC" "2015-03-02 12:00:00 UTC" "2015-03-02 12:15:00 UTC"
 [121] "2015-03-02 12:30:00 UTC" "2015-03-02 12:45:00 UTC" "2015-03-02 13:00:00 UTC"
 [124] "2015-03-02 13:15:00 UTC" "2015-03-02 13:30:00 UTC" "2015-03-02 13:45:00 UTC"
 [127] "2015-03-02 14:00:00 UTC" "2015-03-02 14:15:00 UTC" "2015-03-02 14:30:00 UTC"
 [130] "2015-03-02 14:45:00 UTC" "2015-03-02 15:00:00 UTC" "2015-03-02 15:15:00 UTC"
 [133] "2015-03-02 15:30:00 UTC" "2015-03-02 15:45:00 UTC" "2015-03-02 16:00:00 UTC"
 [136] "2015-03-02 16:15:00 UTC" "2015-03-02 16:30:00 UTC" "2015-03-02 16:45:00 UTC"
 [139] "2015-03-02 17:00:00 UTC" "2015-03-02 17:15:00 UTC" "2015-03-02 17:30:00 UTC"
 [142] "2015-03-02 17:45:00 UTC" "2015-03-02 18:00:00 UTC" "2015-03-02 18:15:00 UTC"
 [145] "2015-03-02 18:30:00 UTC" "2015-03-02 18:45:00 UTC" "2015-03-02 19:00:00 UTC"
 [148] "2015-03-02 19:15:00 UTC" "2015-03-02 19:30:00 UTC" "2015-03-02 19:45:00 UTC"
 [151] "2015-03-02 20:00:00 UTC" "2015-03-02 20:15:00 UTC" "2015-03-02 20:30:00 UTC"
 [154] "2015-03-02 20:45:00 UTC" "2015-03-02 21:00:00 UTC" "2015-03-02 21:15:00 UTC"
 [157] "2015-03-02 21:30:00 UTC" "2015-03-02 21:45:00 UTC" "2015-03-02 22:00:00 UTC"
 [160] "2015-03-02 22:15:00 UTC" "2015-03-02 22:30:00 UTC" "2015-03-02 22:45:00 UTC"
 [163] "2015-03-02 23:00:00 UTC" "2015-03-02 23:15:00 UTC" "2015-03-02 23:30:00 UTC"
 [166] "2015-03-02 23:45:00 UTC" "2015-03-03 00:00:00 UTC" "2015-03-03 00:15:00 UTC"
 ...
 

Есть ли способ построить последовательность так, чтобы итерации с 22:15 по 06:15 исключались для каждого дня? Спасибо!

Ответ №1:

Вот базовый R-хак,

 hour_of_day <- with(as.POSIXlt(dates), hour   min/60)
head(hour_of_day)
# [1] 6.50 6.75 7.00 7.25 7.50 7.75
tail(hour_of_day)
# [1] 20.75 21.00 21.25 21.50 21.75 22.00
range(hour_of_day)
# [1]  0.00 23.75
 

Таким образом, мы можем сделать простое подмножество с этим:

 length(dates)
# [1] 1983
dates2 <- dates[ 6.25 <= hour_of_day amp; hour_of_day <= 22.25 ]
length(dates2)
# [1] 1363
dates[55:65]
#  [1] "2015-03-01 20:00:00 UTC" "2015-03-01 20:15:00 UTC" "2015-03-01 20:30:00 UTC" "2015-03-01 20:45:00 UTC"
#  [5] "2015-03-01 21:00:00 UTC" "2015-03-01 21:15:00 UTC" "2015-03-01 21:30:00 UTC" "2015-03-01 21:45:00 UTC"
#  [9] "2015-03-01 22:00:00 UTC" "2015-03-01 22:15:00 UTC" "2015-03-01 22:30:00 UTC"
dates2[55:65]
#  [1] "2015-03-01 20:00:00 UTC" "2015-03-01 20:15:00 UTC" "2015-03-01 20:30:00 UTC" "2015-03-01 20:45:00 UTC"
#  [5] "2015-03-01 21:00:00 UTC" "2015-03-01 21:15:00 UTC" "2015-03-01 21:30:00 UTC" "2015-03-01 21:45:00 UTC"
#  [9] "2015-03-01 22:00:00 UTC" "2015-03-01 22:15:00 UTC" "2015-03-02 06:15:00 UTC"
 

(Обратите внимание, что с последними двумя элементами он переходит от 22:15:00 к 06:15:00 .)