R - 一个月中给定一周的工作日
R - working days in a given week of a month
我正在尝试获取给定月份的给定周的工作日。
这是我目前的数据,例如:
year month week
2020 6 1
2020 6 2
2020 6 3
2020 6 4
2020 6 5
2020 7 1
2020 7 2
2020 7 3
2020 7 4
2020 7 5
预期结果:
year month week work_days
2020 6 1 5
2020 6 2 5
2020 6 3 5
2020 6 4 5
2020 6 5 2
2020 7 1 3
2020 7 2 5
2020 7 3 5
2020 7 4 5
2020 7 5 5
正如你所看到的,我有年、月和月中的周,但我不知道如何在 R 中获取任何月任何一周的工作日。
提前致谢
cal <- data.table(dates = seq.Date(ymd(20200601), ymd(20200731), by = "day")) %>%
.[, .(dates,
year = year(dates),
month = month(dates),
week = isoweek(dates), # new week starts on monday
weekday = !(weekdays(dates) %in% c("Sunday", "Saturday"))
)]
cal[, .(work_days = sum(weekday)), by = .(year, month, week)
][, week := rowid(month)][]
我正在尝试获取给定月份的给定周的工作日。
这是我目前的数据,例如:
year month week
2020 6 1
2020 6 2
2020 6 3
2020 6 4
2020 6 5
2020 7 1
2020 7 2
2020 7 3
2020 7 4
2020 7 5
预期结果:
year month week work_days
2020 6 1 5
2020 6 2 5
2020 6 3 5
2020 6 4 5
2020 6 5 2
2020 7 1 3
2020 7 2 5
2020 7 3 5
2020 7 4 5
2020 7 5 5
正如你所看到的,我有年、月和月中的周,但我不知道如何在 R 中获取任何月任何一周的工作日。
提前致谢
cal <- data.table(dates = seq.Date(ymd(20200601), ymd(20200731), by = "day")) %>%
.[, .(dates,
year = year(dates),
month = month(dates),
week = isoweek(dates), # new week starts on monday
weekday = !(weekdays(dates) %in% c("Sunday", "Saturday"))
)]
cal[, .(work_days = sum(weekday)), by = .(year, month, week)
][, week := rowid(month)][]