簡體   English   中英

我如何在r中獲得下個月的第一個工作日

[英]How can I get the first business day of the following month in r

嗨,我正在嘗試創建一個值,該值告訴我下個月的第一個工作日。

首先,我創建一個查找表,列出所有前一個工作日:

closedaylookup <-subset(closedaylookup,month.name =“ March”)

名稱(closedaylookup)<-c(“ month”,“ firstbday”)

closedaylookup

月份第一天

2019年1月1日

2019年2月2日

2019年3月3日

2019年4月4日

2019年5月5日

2019年6月6日-06-03

2019年7月7日

2019年8月8日

2019年9月9日

2019年10月10日-10-01

2019年11月11日

2019年12月12日

然后為下個月創建一個向量

date_vector <-function(x){+ as.Date(format(x + 32,“%Y-%m-01”))}

格式(date_vector(Sys.Date()),“%B”)

[1]“三月”

然后我被卡住了,因為我不知道如何構建同時使用date_vector和lookup table的向量

我的目標是創建一個值項目,以顯示下個月的第一個工作日。 我正在將此用作其他分析的要素

誰能幫我 ? 謝謝,

這是一種方法:首先在業務日期中添加month數。 然后,對於任何想要查找的日期,第一個月(使用添加日期%m+% months(1)從lubridate避免側翻,並使用日期做加盟的一個月。這是一個可重復的示例(僅使用問題中的幾個日期)。

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

使用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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM