【问题标题】:R - working days in a given week of a monthR - 一个月中某一周的工作日
【发布时间】:2020-11-05 21:32:55
【问题描述】:

我正在尝试在给定月份获取给定一周的工作日。

这是我目前的数据,例如:

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 中获取任何一个月的任何一周的一周的工作日。

提前致谢

【问题讨论】:

  • 任何考虑到假期。
  • 不,我将分别管理这些。到目前为止,只是为了更容易解决而没有包括它们。

标签: r date lubridate


【解决方案1】:
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)][]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-29
    • 1970-01-01
    • 2021-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-04
    相关资源
    最近更新 更多