我正在尝试使用lubridate作为优化人员计划的基础。如何在不使用日历中的特定日期的情况下为轮班和工作时间编写数据框? (是的,我刚刚开始,并且已经遇到了简单的问题,我是R的新手...)

我手动编写了一些信息,但是我需要能够每天24小时计算一次,因此为了确定起见,我尝试计算工作时间。

shift <- c("week", "evening", "weeknight", "weekend", "weekendnight")
start <- c(hms::as.hms(27000), hms::as.hms(43200), hms::as.hms(77400), hms::as.hms(28800), hms::as.hms(72000))
end <- c(hms::as.hms(64800), hms::as.hms(79200), hms::as.hms(29700), hms::as.hms(72000), hms::as.hms(28800))
hours <- c(hms::as.hms(37800), hms::as.hms(36000), hms::as.hms(38700), hms::as.hms(43200), hms::as.hms(43200))


shiftsDF <- data.frame(shift, start, end, hours) %>%
mutate(computedhours = hms::as.hms(end-start))

我希望“计算小时数”与我手动输入的“小时数”相同,直到我了解到这仅仅是秒数之和,与白天无关。如何让R studio理解我在谈论几天而不必使用确切日期?

最佳答案

使用difftime的基本R方式是,如果添加end,则在start > end中增加一天(86400秒),并使用difftime"hours"为单位获取输出。

with(shiftsDF, ifelse(start > end,
        difftime(as.POSIXct(end) + 86400, as.POSIXct(start), units = "hours") ,
        difftime(end, start, units=  "hours")))

#[1] 10.50 10.00 10.75 12.00 12.00

这将返回小时数比例,其中10.5表示10小时30分钟,10.75表示10小时45分钟,依此类推。

数据
shiftsDF <- data.frame(shift, start, end, hours)

关于r - 如何在没有特定日期且不超过24小时的时间内使用lubridate增加小时数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57047684/

10-12 17:45
查看更多