将开始时间和总持续时间转换为每小时经过的时间

时间:2019-02-12 13:10:25

标签: r time duration

我有关于开始时间(“ startTime”,日期时间变量,POSIXct)和持续时间(以分钟为单位)的数据(“ duration_minutes”):

df <- data.frame(id = c(1, 2, 3),
                 startTime = as.POSIXct(c("2018-01-01 12:15:31",
                                          "2018-01-02 23:43:00",
                                          "2018-01-03 11:00:11")), 
                 duration_minutes = c(315, 120, 45))

我想将开始时间和持续时间转换为每小时的每小时经过时间,从开始时间的小时到持续时间结束时的最后一个小时:

df_result <- data.frame(id = c(1, 1, 1, 1, 1, 1, 2, 2, 2, 3),
                        startTime = c("2018-01-01 12:15:31","2018-01-01 13:00:00",
                                "2018-01-01 14:00:00","2018-01-01 15:00:00",
                                "2018-01-01 16:00:00","2018-01-01 17:00:00",

                                "2018-01-02 23:43:00","2018-01-03 00:00:00",
                                "2018-01-03 01:00:00",

                                "2018-01-03 11:00:11"),
                        duration_minutes = c(44.48, 60, 60, 60, 60, 30.5, 17, 60, 43, 45))

请提出可能的解决方案。

2 个答案:

答案 0 :(得分:0)

尝试一下:

library(data.table)
library(lubridate)
library(magrittr)

df <-
  setDT(df)[, start_ceiling := ceiling_date(startTime, "hour", change_on_boundary = TRUE)] %>%
  .[, `:=` (
    reps = ifelse(
      startTime + (duration_minutes * 60) <= start_ceiling, 1, pmax(2, floor(duration_minutes / 60) + 1)
    ),
    initial_diff = as.numeric(difftime(start_ceiling[1], startTime[1], units = "mins"))
  ), by = id] %>%
  .[, df[df[, rep(.I, reps)]]] %>%
  .[, startTime := pmax(startTime, floor_date(startTime, "hour") + hours(0:(.N - 1))), by = id] %>%
  .[reps > 1, duration_minutes := c(initial_diff[.N], 
                                    rep(60, reps[.N] - 2),
                                    (duration_minutes[.N] - initial_diff[.N]) %% 60), by = id] %>%
  .[!(duration_minutes == 0 & reps > 1), ] %>%
  .[, c("reps", "start_ceiling", "initial_diff") := NULL]

我已经用我们到目前为止收集的所有场景测试了这一点,这是输出:

    id           startTime duration_minutes
 1:  1 2018-01-01 12:15:31         44.48333
 2:  1 2018-01-01 13:00:00         60.00000
 3:  1 2018-01-01 14:00:00         60.00000
 4:  1 2018-01-01 15:00:00         60.00000
 5:  1 2018-01-01 16:00:00         60.00000
 6:  1 2018-01-01 17:00:00         30.51667
 7:  2 2018-01-02 23:43:00         17.00000
 8:  2 2018-01-03 00:00:00         60.00000
 9:  2 2018-01-03 01:00:00         43.00000
10:  3 2018-01-03 11:00:11         45.00000
11:  4 2018-01-04 10:00:00         60.00000
12:  4 2018-01-04 11:00:00          5.00000
13:  5 2018-01-05 00:00:00         60.00000
14:  6 2018-01-06 11:35:00         25.00000
15:  6 2018-01-06 12:00:00         10.00000
16:  7 2018-01-07 00:00:00         60.00000
17:  7 2018-01-07 01:00:00         60.00000

使用的数据:

df <- data.frame(
  id = c(1, 2, 3, 4, 5, 6, 7),
  startTime = as.POSIXct(
    c(
      "2018-01-01 12:15:31",
      "2018-01-02 23:43:00",
      "2018-01-03 11:00:11",
      "2018-01-04 10:00:00",
      "2018-01-05 00:00:00",
      "2018-01-06 11:35:00",
      "2018-01-07 00:00:00"
    )
  ),
  duration_minutes = c(315, 120, 45, 65, 60, 35, 120)
)

df

  id           startTime duration_minutes
1  1 2018-01-01 12:15:31              315
2  2 2018-01-02 23:43:00              120
3  3 2018-01-03 11:00:11               45
4  4 2018-01-04 10:00:00               65
5  5 2018-01-05 00:00:00               60
6  6 2018-01-06 11:35:00               35
7  7 2018-01-07 00:00:00              120

答案 1 :(得分:0)

另一种可能性:

library(data.table)
library(lubridate)

setDT(df)
df[ , ceil_start := ceiling_date(start, "hour", change_on_boundary = TRUE)]

df[ , {
  if(difftime(ceil_start, start, units = "min") > dur) {
    .SD[ , .(start, dur)]
  } else {
    end <- start + dur * 60
    time <- c(start,
              seq(from = ceil_start,
                  to = floor_date(end, "hour"),
                  by = "hour"),
              end)
    .(start = head(time, -1), dur = `units<-`(diff(time), "mins"))
  }
},
by = id]

#     id               start           dur
# 1:   1 2018-01-01 12:15:31 44.48333 mins
# 2:   1 2018-01-01 13:00:00 60.00000 mins
# 3:   1 2018-01-01 14:00:00 60.00000 mins
# 4:   1 2018-01-01 15:00:00 60.00000 mins
# 5:   1 2018-01-01 16:00:00 60.00000 mins
# 6:   1 2018-01-01 17:00:00 30.51667 mins
# 7:   2 2018-01-02 23:43:00 17.00000 mins
# 8:   2 2018-01-03 00:00:00 60.00000 mins
# 9:   2 2018-01-03 01:00:00 43.00000 mins
# 10:  3 2018-01-03 11:00:11 45.00000 mins
# 11:  4 2018-01-03 11:35:00 25.00000 mins
# 12:  4 2018-01-03 12:00:00 10.00000 mins
# 13:  5 2018-01-03 00:00:00 60.00000 mins
# 14:  5 2018-01-03 01:00:00  0.00000 mins

说明

data.frame转换为data.tablesetDT)。将开始时间四舍五入到最接近的小时数(ceiling_date(start, "hour", ...)。使用change_on_boundary = TRUE可以更轻松地处理没有分钟和几秒钟的时间(不在数据中,但经过测试)。

要处理结束时间(开始+持续时间)与开始时间(例如id = 3)在同一小时内的情况,请检查舍入时间与开始时间之间的差是否大于持续时间(if(difftime(ceil_start, start, units = "min") > dur)) )。如果是这样,只需选择开始和持续时间列(.SD[ , .(start, dur))。

对于其他情况(else),请计算结束时间:end <- start + dur * 60。从向上舍入的开始时间('ceil_start')到向下舍入的结束时间创建一个序列,并按小时递增(seq(from = ceil_start, to = floor_date(end, "hour"), by = "hour"))。与“开始”和“结束”时间串联。返回除最后一个(head(time, -1)以外的所有时间,并计算以分钟为单位的时间步长(`units<-`(diff(time), "mins"))。

对于H:M:S = 00:00:00且持续时间为60分钟的倍数的时间(如id = 5),当前解决方案给出最后一个小时持续时间为0分钟的行。在等待更优雅的解决方案时,一种快速而肮脏的方法就是删除工期= 0的此类行。


数据

请注意,我添加了一个不包含在原始数据中的案例,id = 4(另请参见my comment above)和id = 5。

df <- data.frame(id = 1:5,
                 start = as.POSIXct(c("2018-01-01 12:15:31",
                                      "2018-01-02 23:43:00",
                                      "2018-01-03 11:00:11",
                                      "2018-01-03 11:35:00",
                                      "2018-01-03 00:00:00")), 
                 dur = c(315, 120, 45, 35, 60))