Установите определенную метку времени для строк с определенными характеристиками в R

#r #date #timestamp

Вопрос:

У меня есть данные, которые выглядят следующим образом

 | Resource |  start   |   end    |  day  |
| -------- | -------- | -------- | ----- |
|    C1    | 01:00:00 | 00:59:59 |  Mon  |
|    C1    | 01:00:00 | 02:06:00 |  Tue  |
|    C1    | 02:06:00 | 01:04:00 |  Tue  |
|    C1    | 01:00:00 | 06:20:00 |  Wed  |
|    C1    | 06:20:00 | 23:34:00 |  Wed  |
|    C1    | 23:34:00 | 00:59:59 |  Wed  |
|    C1    | 01:00:00 | 22:36:00 |  Thu  |
|    C1    | 22:36:00 | 00:59:59 |  Mon  |
 

В третьей строке в расписании есть ошибка, так как время окончания раньше времени начала. Я хотел бы просмотреть все дни и убедиться, что последняя отметка времени окончания каждого дня равна 00:59:59
В конце фрейм данных должен выглядеть так.

 | Resource |  start   |   end    |  day  |
| -------- | -------- | -------- | ----- |
|    C1    | 01:00:00 | 00:59:59 |  Mon  |
|    C1    | 01:00:00 | 02:06:00 |  Tue  |
|    C1    | 02:06:00 | 00:59:59 |  Tue  |
|    C1    | 01:00:00 | 06:20:00 |  Wed  |
|    C1    | 06:20:00 | 23:34:00 |  Wed  |
|    C1    | 23:34:00 | 00:59:59 |  Wed  |
|    C1    | 01:00:00 | 22:36:00 |  Thu  |
|    C1    | 22:36:00 | 00:59:59 |  Mon  |
 

В реальных данных у меня есть несколько ресурсов C1, C2, C3, S1 и т. Д. Так что это следует принимать во внимание. Последняя отметка времени окончания для каждого ресурса на каждый день (Пн, Вт, Ср…) должна быть 00:59:59

ОБНОВЛЕНИЕ: Я пытаюсь закодировать предложение в комментариях, но я не знаю, как создать функцию карты. Я пытался:

 HelpFunction <- function(x){
  h <- hms(x)
  x <- ifelse(h<lag(h), hms("23:59:59"), x)
}
map(df$period_end, HelpFunction)
 

но это не работает. Есть ли у вас какие-либо предложения по конкретной реализации?

Данные:

 structure(list(resource = c("SolverC1", "SolverC1", "SolverC1", 
"SolverC1", "SolverC1", "SolverC1", "SolverC1", "SolverC1", "SolverC1", 
"SolverC1", "SolverC1", "SolverC1", "SolverC2", "SolverC2", "SolverC2", 
"SolverC2", "SolverC2", "SolverC2", "SolverC2", "SolverC2", "SolverC2", 
"SolverC2", "SolverC2", "SolverC2", "SolverC2", "SolverC2", "SolverC2", 
"SolverC2", "SolverC3", "SolverC3", "SolverC3", "SolverC3", "SolverC3", 
"SolverC3", "SolverC3", "SolverC3", "SolverC3", "SolverC3", "SolverC3", 
"SolverC3", "SolverC3", "SolverS1", "SolverS1", "SolverS1", "SolverS1", 
"SolverS1", "SolverS1", "SolverS1", "SolverS1", "SolverS1", "SolverS1", 
"SolverS1", "SolverS1", "SolverS1", "SolverS1", "SolverS1", "SolverS1", 
"SolverS1", "SolverS2", "SolverS2", "SolverS2", "SolverS2", "SolverS2", 
"SolverS2", "SolverS2", "SolverS2", "SolverS2", "SolverS2", "SolverS2", 
"SolverS2", "SolverS2", "SolverS2", "SolverS2", "SolverS2", "SolverS2", 
"SolverS2", "SolverS3", "SolverS3", "SolverS3", "SolverS3", "SolverS3", 
"SolverS3", "SolverS3", "SolverS3", "SolverS3", "SolverS3", "SolverS3", 
"SolverS3", "SolverS3", "SolverS3", "SolverS3", "SolverS3", "SolverS3", 
"System", "System", "System", "System", "System", "System", "System", 
"System", "Tester1", "Tester1", "Tester1", "Tester1", "Tester1", 
"Tester1", "Tester1", "Tester1", "Tester1", "Tester1", "Tester1", 
"Tester1", "Tester1", "Tester2", "Tester2", "Tester2", "Tester2", 
"Tester2", "Tester2", "Tester2", "Tester2", "Tester2", "Tester2", 
"Tester2", "Tester2", "Tester2", "Tester2", "Tester2", "Tester2", 
"Tester3", "Tester3", "Tester3", "Tester3", "Tester3", "Tester3", 
"Tester3", "Tester3", "Tester3", "Tester3", "Tester3", "Tester4", 
"Tester4", "Tester4", "Tester4", "Tester4", "Tester4", "Tester4", 
"Tester4", "Tester4", "Tester4", "Tester4", "Tester4", "Tester4", 
"Tester4", "Tester4", "Tester4", "Tester4", "Tester5", "Tester5", 
"Tester5", "Tester5", "Tester5", "Tester5", "Tester5", "Tester5", 
"Tester5", "Tester5", "Tester5", "Tester5", "Tester5", "Tester5", 
"Tester5", "Tester6", "Tester6", "Tester6", "Tester6", "Tester6", 
"Tester6", "Tester6", "Tester6", "Tester6", "Tester6", "Tester6", 
"Tester6"), period_start = c("01:00:00", "01:00:00", "01:00:00", 
"03:10:00", "22:05:00", "01:00:00", "07:18:00", "18:30:00", "01:00:00", 
"04:26:00", "01:00:00", "01:00:00", "01:00:00", "02:16:00", "01:00:00", 
"02:35:00", "23:18:00", "01:00:00", "19:05:00", "01:00:00", "03:07:00", 
"20:29:00", "01:00:00", "01:00:00", "06:10:00", "01:00:00", "03:51:00", 
"20:24:00", "01:00:00", "01:00:00", "07:38:00", "23:38:00", "01:00:00", 
"05:10:00", "01:00:00", "02:39:00", "23:59:00", "01:00:00", "01:00:00", 
"03:20:00", "23:28:00", "01:00:00", "03:38:00", "23:21:00", "01:00:00", 
"03:57:00", "01:00:00", "02:46:00", "21:15:00", "01:00:00", "02:20:00", 
"20:21:00", "19:55:00", "01:00:00", "03:53:00", "08:41:00", "01:00:00", 
"21:07:00", "01:00:00", "08:15:00", "01:00:00", "02:12:00", "01:00:00", 
"02:01:00", "22:50:00", "01:00:00", "11:54:00", "21:26:00", "01:00:00", 
"02:13:00", "23:50:00", "01:00:00", "23:53:00", "01:00:00", "02:10:00", 
"22:51:00", "01:00:00", "03:01:00", "01:00:00", "07:48:00", "01:00:00", 
"03:07:00", "22:14:00", "01:00:00", "13:34:00", "21:34:00", "01:00:00", 
"03:48:00", "01:00:00", "23:06:00", "01:00:00", "04:34:00", "23:25:00", 
"01:00:00", "01:00:00", "01:00:00", "01:00:00", "02:33:00", "01:00:00", 
"01:00:00", "01:00:00", "01:00:00", "02:46:00", "22:38:00", "01:00:00", 
"01:00:00", "03:17:00", "21:17:00", "01:00:00", "03:52:00", "01:00:00", 
"01:00:00", "01:00:00", "22:56:00", "01:00:00", "02:53:00", "01:00:00", 
"02:26:00", "01:00:00", "02:43:00", "22:05:00", "01:00:00", "07:35:00", 
"21:08:00", "01:00:00", "02:05:00", "01:00:00", "06:20:00", "01:00:00", 
"02:34:00", "01:00:00", "02:56:00", "01:00:00", "22:40:00", "01:00:00", 
"01:00:00", "04:24:00", "21:55:00", "01:00:00", "01:00:00", "01:00:00", 
"01:00:00", "22:50:00", "01:00:00", "02:07:00", "01:00:00", "20:36:00", 
"01:00:00", "02:21:00", "01:00:00", "04:39:00", "23:35:00", "01:00:00", 
"02:50:00", "08:46:00", "01:00:00", "02:29:00", "23:36:00", "01:00:00", 
"01:00:00", "03:08:00", "23:44:00", "01:00:00", "02:34:00", "23:56:00", 
"01:00:00", "03:20:00", "01:00:00", "03:52:00", "23:32:00", "01:00:00", 
"02:05:00", "01:00:00", "01:00:00", "03:15:00", "23:59:00", "01:00:00", 
"01:00:00", "03:35:00", "22:00:00", "01:00:00", "02:34:00", "23:11:00", 
"01:00:00", "01:00:00"), period_end = c("00:59:59", "00:59:59", 
"03:10:00", "22:05:00", "00:59:59", "07:18:00", "18:30:00", "00:59:59", 
"04:26:00", "00:59:59", "00:59:59", "00:59:59", "02:16:00", "00:59:59", 
"02:35:00", "23:18:00", "00:59:59", "19:05:00", "00:59:59", "03:07:00", 
"20:29:00", "00:59:59", "01:16:00", "06:10:00", "00:59:59", "03:51:00", 
"20:24:00", "00:59:59", "18:49:00", "07:38:00", "23:38:00", "00:59:59", 
"05:10:00", "00:59:59", "02:39:00", "00:59:59", "00:59:59", "00:59:59", 
"03:20:00", "23:28:00", "00:59:59", "03:38:00", "23:21:00", "00:59:59", 
"03:57:00", "00:59:59", "02:46:00", "21:15:00", "00:59:59", "02:20:00", 
"20:21:00", "00:59:59", "00:59:59", "03:53:00", "08:41:00", "00:59:59", 
"21:07:00", "00:59:59", "08:15:00", "00:59:59", "02:12:00", "00:59:59", 
"02:01:00", "22:50:00", "00:59:59", "11:54:00", "21:26:00", "00:59:59", 
"02:13:00", "23:50:00", "00:59:59", "23:53:00", "00:59:59", "02:10:00", 
"22:51:00", "00:59:59", "03:01:00", "00:59:59", "07:48:00", "00:59:59", 
"03:07:00", "22:14:00", "00:59:59", "13:34:00", "21:34:00", "00:59:59", 
"03:48:00", "23:55:00", "23:06:00", "00:59:59", "04:34:00", "23:25:00", 
"00:59:59", "00:59:59", "00:59:59", "00:59:59", "02:33:00", "00:59:59", 
"00:59:59", "00:59:59", "00:59:59", "02:46:00", "22:38:00", "00:59:59", 
"00:59:59", "03:17:00", "21:17:00", "00:59:59", "03:52:00", "00:59:59", 
"00:59:59", "00:59:59", "22:56:00", "00:59:59", "02:53:00", "00:59:59", 
"02:26:00", "00:59:59", "02:43:00", "22:05:00", "00:59:59", "07:35:00", 
"21:08:00", "00:59:59", "02:05:00", "01:03:00", "06:20:00", "00:59:59", 
"02:34:00", "00:59:59", "02:56:00", "00:59:59", "22:40:00", "00:59:59", 
"00:59:59", "04:24:00", "21:55:00", "00:59:59", "00:59:59", "00:59:59", 
"00:59:59", "22:50:00", "00:59:59", "02:07:00", "00:59:59", "20:36:00", 
"00:59:59", "02:21:00", "00:59:59", "04:39:00", "23:35:00", "00:59:59", 
"02:50:00", "08:46:00", "00:59:59", "02:29:00", "23:36:00", "00:59:59", 
"22:26:00", "03:08:00", "23:44:00", "00:59:59", "02:34:00", "23:56:00", 
"00:59:59", "03:20:00", "00:59:59", "03:52:00", "23:32:00", "00:59:59", 
"02:05:00", "00:59:59", "00:59:59", "03:15:00", "23:59:00", "00:59:59", 
"01:06:00", "03:35:00", "22:00:00", "00:59:59", "02:34:00", "00:59:59", 
"00:59:59", "00:59:59", "00:59:59"), status = c("available", 
"available", "unavailable", "available", "unavailable", "unavailable", 
"available", "unavailable", "unavailable", "available", "available", 
"available", "unavailable", "available", "unavailable", "available", 
"unavailable", "available", "unavailable", "unavailable", "available", 
"unavailable", "available", "available", "unavailable", "unavailable", 
"available", "unavailable", "available", "unavailable", "available", 
"unavailable", "unavailable", "available", "unavailable", "available", 
"unavailable", "available", "unavailable", "available", "unavailable", 
"unavailable", "available", "unavailable", "unavailable", "available", 
"unavailable", "available", "unavailable", "unavailable", "available", 
"unavailable", "unavailable", "unavailable", "available", "unavailable", 
"available", "unavailable", "unavailable", "available", "unavailable", 
"available", "unavailable", "available", "unavailable", "unavailable", 
"available", "unavailable", "unavailable", "available", "unavailable", 
"available", "unavailable", "unavailable", "available", "unavailable", 
"unavailable", "available", "unavailable", "available", "unavailable", 
"available", "unavailable", "unavailable", "available", "unavailable", 
"unavailable", "available", "available", "unavailable", "unavailable", 
"available", "unavailable", "available", "available", "available", 
"unavailable", "available", "available", "available", "available", 
"unavailable", "available", "unavailable", "available", "unavailable", 
"available", "unavailable", "unavailable", "available", "available", 
"available", "available", "unavailable", "unavailable", "available", 
"unavailable", "available", "unavailable", "available", "unavailable", 
"unavailable", "available", "unavailable", "unavailable", "available", 
"available", "unavailable", "unavailable", "available", "unavailable", 
"available", "available", "unavailable", "available", "unavailable", 
"available", "unavailable", "available", "available", "available", 
"available", "unavailable", "unavailable", "available", "available", 
"unavailable", "unavailable", "available", "unavailable", "available", 
"unavailable", "unavailable", "available", "unavailable", "unavailable", 
"available", "unavailable", "available", "unavailable", "available", 
"unavailable", "unavailable", "available", "unavailable", "unavailable", 
"available", "unavailable", "available", "unavailable", "unavailable", 
"available", "available", "unavailable", "available", "unavailable", 
"available", "unavailable", "available", "unavailable", "unavailable", 
"available", "unavailable", "available", "available"), day = structure(c(1L, 
2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 6L, 7L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 4L, 4L, 4L, 5L, 6L, 6L, 7L, 7L, 7L, 1L, 2L, 2L, 2L, 3L, 
3L, 4L, 4L, 5L, 6L, 7L, 7L, 7L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 
4L, 4L, 4L, 5L, 6L, 6L, 6L, 7L, 7L, 1L, 1L, 2L, 2L, 3L, 3L, 3L,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 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L)), row.names = c("19", "6", 
"3810", "21", "3910", "1", "1100", "2100", "2910", "16", "10", 
"18", "611", "34", "631", "35", "641", "36", "661", "5510", "30", 
"5610", "25", "45", "841", "591", "33", "601", "64", "941", "51", 
"951", "1081", "59", "1101", "60", "1251", "55", "116", "63", 
"1171", "1641", "85", "1651", "1381", "73", "1401", "74", "1411", 
"1561", "81", "1571", "1451", "1741", "90", "1751", "71", "1351", 
"2121", "109", "2141", "110", "2161", "111", "2171", "1761", 
"91", "1771", "1781", "92", "1791", "107", "209", "1821", "94", 
"1831", "2451", "126", "2471", "127", "2351", "121", "2361", 
"2241", "1151", "2251", "2531", "130", "124", "2421", "2431", 
"125", "2441", "149", "143", "158", "2811", "145", "146", "147", 
"141", "324", "167", "325", "168", "328", "169", "329", "330", 
"170", "184", "178", "172", "336", "380", "193", "382", "194", 
"384", "195", "385", "363", "186", "364", "365", "187", "205", 
"404", "367", "188", "426", "216", "224", "443", "218", "446", 
"226", "447", "207", "214", "222", "249", "487", "488", "250", 
"251", "491", "478", "245", "480", "246", "481", "496", "254", 
"497", "484", "248", "485", "273", "508", "260", "509", "5101", 
"261", "5111", "512", "262", "541", "276", "542", "516", "264", 
"2581", "578", "295", "579", "282", "555", "283", "556", "557", 
"284", "547", "286", "280"), class = "data.frame")
 

Комментарии:

1. Вы хотите перебрать свои данные и протестировать эти условия для каждого «дневного ящика». Таким образом, сгруппируйте свой фрейм данных по ресурсам, дням и неделям, чтобы иметь эти ячейки. Определите функцию, которая проверяет, было ли предыдущее время окончания до/равно следующему времени начала, и убедитесь, что последнее время окончания соответствует желаемому 00:59:59 (в противном случае перезапишите ошибочную метку времени). Вы можете выполнить итерацию такой функции над вашим сгруппированным фреймом данных с помощью purrr::map().

2. Извините, если я неправильно понял, или, может быть, я немного туповат, но как «Пн 00:59:59» (Пн 12:59 утра) после Пн 01:00:00″ (Пн 01:00 утра) ?

3. @dario Я использовал один алгоритм из Stackoverflow, который, к сожалению, допускает ошибку, предполагая, что день начинается в 01:01, а не в 00:01. Это также создает мне проблемы при выполнении итерации. Так что, если у кого-нибудь из вас есть идея, как реализовать то, что предложил Рэй, я был бы признателен