% 选择(v_v_启动,v_v_完成)v_v_i-6ren">
gpt4 book ai didi

R如何仅计算营业时间的 "task time"

转载 作者:行者123 更新时间:2023-12-04 15:13:34 25 4
gpt4 key购买 nike

有没有办法只计算工作时间的“任务时间”?工作时间 8 点到 5 点,周一到周五。示例:

使用datediff():

enter image description here

预期结果:

enter image description here

示例任务时间:df %>%

  • 选择(v_v_启动,v_v_完成)v_v_initiated v_v_complete1 2020-04-23 14:13:52.0000000 2020-04-23 16:04:28.00000002 2020-11-10 11:48:53.0000000 2020-11-10 13:12:31.00000003 2020-10-20 16:03:39.0000000 2020-10-20 16:25:16.00000004 2020-04-02 13:43:54.0000000 2020-04-02 14:14:45.00000005 2020-07-09 08:52:54.0000000 2020-07-23 09:18:29.00000006 2020-06-09 14:56:33.0000000 2020-06-10 07:44:17.00000007 2020-09-17 15:11:39.0000000 2020-09-17 15:13:41.00000008 2020-10-28 14:08:20.0000000 2020-10-28 14:07:35.00000009 2020-04-21 12:55:36.0000000 2020-04-27 12:56:17.000000010 2020-11-06 11:02:03.0000000 2020-11-06 11:02:30.000000011 2020-02-17 12:29:21.0000000 2020-02-18 12:52:23.000000012 2020-08-25 15:25:46.0000000 2020-08-26 10:18:26.000000013 2020-02-19 15:05:28.0000000 2020-02-20 09:43:48.000000014 2020-09-23 21:19:41.0000000 2020-09-24 14:52:21.000000015 2020-07-01 14:20:11.0000000 2020-07-01 14:20:59.000000016 2020-05-01 15:22:58.0000000 2020-05-01 16:32:35.000000017 2020-06-29 13:10:58.0000000 2020-06-30 13:53:29.000000018 2020-06-16 12:56:54.0000000 2020-06-16 14:27:15.000000019 2020-03-27 11:02:29.0000000 2020-03-30 15:18:51.000000020 2020-04-08 07:38:01.0000000 2020-04-08 07:52:35.000000021 2020-07-30 09:32:42.0000000 2020-07-30 10:32:28.000000022 2020-06-17 14:03:31.0000000 2020-07-10 15:38:03.000000023 2020-04-24 10:41:27.0000000 2020-04-29 13:07:05.000000024 2020-08-26 10:41:10.0000000 2020-08-26 12:55:23.000000025 2020-10-26 18:11:16.0000000 2020-10-27 16:10:39.000000026 2020-01-08 11:12:49.0000000 2020-01-09 09:18:37.000000027 2020-04-17 11:40:10.0000000 2020-04-17 15:51:21.000000028 2020-02-11 10:38:21.0000000 2020-02-11 10:33:54.000000029 2020-03-23 12:10:21.0000000 2020-03-23 12:33:06.000000030 2020-06-02 12:44:00.0000000 2020-06-03 08:28:05.000000031 2020-04-13 09:30:31.0000000 2020-04-13 13:16:55.000000032 2020-04-07 17:36:02.0000000 2020-04-07 17:36:44.000000033 2020-01-15 12:24:42.0000000 2020-01-15 12:25:00.000000034 2020-08-18 08:55:58.0000000 2020-08-18 09:02:34.000000035 2020-07-06 14:10:23.0000000 2020-07-07 10:28:05.000000036 2020-03-25 15:03:20.0000000 2020-03-31 14:17:43.000000037 2020-01-29 12:58:33.0000000 2020-02-14 09:53:06.000000038 2020-02-07 15:11:21.0000000 2020-02-10 09:13:53.000000039 2020-07-27 17:51:13.0000000 2020-07-29 11:52:51.000000040 2020-09-02 11:43:02.0000000 2020-09-02 13:10:46.000000041 2020-07-22 11:04:50.0000000 2020-07-22 11:12:34.000000042 2020-06-29 13:57:17.0000000 2020-06-30 07:34:55.000000043 2020-07-21 10:46:58.0000000 2020-07-21 16:15:59.000000044 2020-05-27 07:38:46.0000000 2020-05-27 07:51:24.000000045 2020-07-14 10:33:49.0000000 2020-07-14 11:38:28.000000046 2020-06-04 16:59:09.0000000 2020-06-09 10:49:20.0000000

最佳答案

您可以调整另一个函数来计算一个时间间隔的营业时间(例如 this

首先,创建一个从开始到结束的日期序列,并仅按工作日过滤。

接下来,使用感兴趣的营业时间创建时间间隔(在本例中为“08:00”到“17:00”)。

确定每天有多少工作时间与您的时间重叠。这样,如果时间从“09:05”开始,则该时间将用于一天的开始,而不是“08:00”。

最后,总结时间间隔,并确定工作天数(假设一天 9 小时),以及剩余的小时和分钟。

如果您想将此函数应用于数据框中的行,您可以使用 mapply,如下所示:

df$business_hours <- mapply(calc_bus_hours, df$start_date, df$end_date)

希望对您有所帮助。

library(lubridate)
library(dplyr)

calc_bus_hours <- function(start, end) {
my_dates <- seq.Date(as.Date(start), as.Date(end), by = "day")
my_dates <- my_dates[!weekdays(my_dates) %in% c("Saturday", "Sunday")]

my_intervals <- interval(ymd_hm(paste(my_dates, "08:00"), tz = "UTC"), ymd_hm(paste(my_dates, "17:00"), tz = "UTC"))

int_start(my_intervals[1]) <- pmax(pmin(start, int_end(my_intervals[1])), int_start(my_intervals[1]))
int_end(my_intervals[length(my_intervals)]) <- pmax(pmin(end, int_end(my_intervals[length(my_intervals)])),
int_start(my_intervals[length(my_intervals)]))

total_time <- sum(time_length(my_intervals, "minutes"))

total_days <- total_time %/% (9 * 60)
total_hours <- total_time %% (9 * 60) %/% 60
total_minutes <- total_time - (total_days * 9 * 60) - (total_hours * 60)
paste(total_days, "days,", total_hours, "hours,", total_minutes, "minutes")
}

calc_bus_hours(as.POSIXct("11/4/2020 9:05", format = "%m/%d/%Y %H:%M", tz = "UTC"),
as.POSIXct("11/9/2020 11:25", format = "%m/%d/%Y %H:%M", tz = "UTC"))

[1] "3 days, 2 hours, 20 minutes"

编辑:如@DPH 所述,假期和部分假期更复杂。

您可以创建假期数据框并指明开放时间,允许部分假期(例如,平安夜从上午 8:00 到中午)。

这里是一个修改后的函数,应该会给出可比较的结果。

library(lubridate)
library(dplyr)

holiday_df <- data.frame(
date = as.Date(c("2020-12-24", "2020-12-25", "2020-12-31", "2020-01-01")),
start = c("08:00", "08:00", "08:00", "08:00"),
end = c("12:00", "08:00", "08:00", "08:00")
)

calc_bus_hours <- function(start, end) {
my_dates <- seq.Date(as.Date(start), as.Date(end), by = "day")

my_dates_df <- data.frame(
date = my_dates[!weekdays(my_dates) %in% c("Saturday", "Sunday")],
start = "08:00",
end = "17:00"
)

all_dates <- union_all(
inner_join(my_dates_df["date"], holiday_df),
anti_join(my_dates_df, holiday_df["date"])
) %>%
arrange(date)

my_intervals <- interval(ymd_hm(paste(all_dates$date, all_dates$start), tz = "UTC"),
ymd_hm(paste(all_dates$date, all_dates$end), tz = "UTC"))

int_start(my_intervals[1]) <- pmax(pmin(start, int_end(my_intervals[1])), int_start(my_intervals[1]))
int_end(my_intervals[length(my_intervals)]) <- pmax(pmin(end, int_end(my_intervals[length(my_intervals)])),
int_start(my_intervals[length(my_intervals)]))

total_time <- sum(time_length(my_intervals, "minutes"))

total_days <- total_time %/% (9 * 60)
total_hours <- total_time %% (9 * 60) %/% 60
total_minutes <- total_time - (total_days * 9 * 60) - (total_hours * 60)
paste(total_days, "days,", total_hours, "hours,", total_minutes, "minutes")
}

关于R如何仅计算营业时间的 "task time",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64739264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com