【问题标题】:How can I get the first business day of the following month in r如何在 r 中获得下个月的第一个工作日
【发布时间】:2019-02-27 19:39:59
【问题描述】:

您好,我正在尝试创建一个值,告诉我下个月的第一个工作日是什么。

首先,我创建一个列出所有第一个工作日的查找表:

closedaylookup

names(closedaylookup)

closedaylookup

一个月的第一天

2019 年 1 月 1 日-01-02

2019 年 2 月 2 日-02-01

2019 年 3 月 3 日-03-01

2019 年 4 月 4 日-04-01

2019 年 5 月 5 日-05-01

2019 年 6 月 6 日-06-03

2019 年 7 月 7 日-07-01

2019 年 8 月 8 日-08-01

2019 年 9 月 9 日-09-03

2019 年 10 月 10 日 10 月 1 日

2019 年 11 月 11 日-11-01

2019 年 12 月 12 日-12-02

然后我为下个月创建一个向量

日期向量

格式(date_vector(Sys.Date()),"%B" )

[1] “三月”

然后我被卡住了,因为我不知道如何构建一个同时利用 date_vector 和查找表的向量

我的目标是创建一个显示下个月第一个工作日的价值项目。我将其用作其他分析的元素

谁能帮帮我? 谢谢,

【问题讨论】:

    标签: r


    【解决方案1】:

    这是一种方法:首先在营业日期中添加一个month 号码。然后,对于您要查找的任何日期,首先在日期上添加一个月(使用 lubridate 中的 %m+% months(1) 以避免翻转,并使用 那个 日期的月份进行连接。这是一个可重复的示例(仅使用问题中的几个日期)。

    library(tidyverse)
    library(lubridate)
    
    df_close_day <- data.frame(
      month = c("January", "Feburary", "March", "April"),
      first_biz_day = ymd(c("2019-01-02", "2019-02-01", "2019-03-01", "2019-04-01"))
    )
    
    # Add the month so that we can join this later
    df_close_day <- df_close_day %>%
      mutate(
        month_of_first_biz_day = month(first_biz_day)
      )
    
    df_dates_to_lookup <- data.frame(
      orig_date = ymd(c("2018-12-21", "2019-01-01", "2019-01-31", "2019-02-15"))
    )
    
    df_dates_to_lookup %>%
      mutate(
        next_month = orig_date %m+% months(1),
        month_of_next = month(next_month)
      ) %>%
      left_join(
        df_close_day, by = c("month_of_next" = "month_of_first_biz_day")
      )
    #>    orig_date next_month month_of_next    month first_biz_day
    #> 1 2018-12-21 2019-01-21             1  January    2019-01-02
    #> 2 2019-01-01 2019-02-01             2 Feburary    2019-02-01
    #> 3 2019-01-31 2019-02-28             2 Feburary    2019-02-01
    #> 4 2019-02-15 2019-03-15             3    March    2019-03-01
    

    【讨论】:

      【解决方案2】:

      使用tidyverselubridate,您可以轻松地处理和合并两个日期列表:

      (data_org <- (tibble(org = ymd(c("2018-12-21", "2019-01-01", "2019-01-31", "2019-02-15"))) %>%
        mutate(month = month(org),
               year = year(org))))
      
      # A tibble: 4 x 3
        org        month  year
        <date>     <dbl> <dbl>
      1 2018-12-21    12  2018
      2 2019-01-01     1  2019
      3 2019-01-31     1  2019
      4 2019-02-15     2  2019
      
      (data_fbd <- tibble(fbd = ymd(c("2019-01-02", "2019-02-01", "2019-03-01", "2019-04-01"))) %>%
        mutate(month = month(fbd) - 1, # adapt month to previous month
               year = year(fbd)) %>%
        mutate(year = case_when(month == 0 ~ year - 1, TRUE ~ year), # adjust year to previous year if previous month is 0 (i.e. 12)
               month = case_when(month == 0 ~ 12, TRUE ~ month))) # adjust month to 12 if previous month is 0 (i.e. 12)
      
      # A tibble: 4 x 3
        fbd        month  year
        <date>     <dbl> <dbl>
      1 2019-01-02    12  2018
      2 2019-02-01     1  2019
      3 2019-03-01     2  2019
      4 2019-04-01     3  2019
      
      left_join(data_org, data_fbd)
      Joining, by = c("month", "year")
      # A tibble: 4 x 4
        org        month  year fbd       
        <date>     <dbl> <dbl> <date>    
      1 2018-12-21    12  2018 2019-01-02
      2 2019-01-01     1  2019 2019-02-01
      3 2019-01-31     1  2019 2019-02-01
      4 2019-02-15     2  2019 2019-03-01
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多