【问题标题】:How to create a dataframe of the holidays in R?如何在 R 中创建假期的数据框?
【发布时间】:2022-01-10 16:57:27
【问题描述】:

我需要从以下命令构建一个 df:

hld   <- tis::holidays(c(
  2010,
  2011,
  2013,
  2014,
  2015,
  2016,
  2017,
  2018,
  2019,
  2020,
  2021,
  2022
))  
  
hld

>> 
    NewYears       MLKing   GWBirthday     Memorial Independence        Labor     Columbus     Veterans Thanksgiving       MLKing   GWBirthday 
    20100101     20100118     20100215     20100531     20100705     20100906     20101011     20101111     20101125     20110117     20110221 
    Memorial Independence        Labor     Columbus     Veterans Thanksgiving    Christmas     NewYears       MLKing   GWBirthday     Memorial 
    20110530     20110704     20110905     20111010     20111111     20111124     20111226     20130101     20130121     20130218     20130527 
Independence        Labor     Columbus     Veterans Thanksgiving    Christmas     NewYears       MLKing   GWBirthday     Memorial Independence 
    20130704     20130902     20131014     20131111     20131128     20131225     20140101     20140120     20140217     20140526     20140704 
       Labor     Columbus     Veterans Thanksgiving    Christmas     NewYears       MLKing   GWBirthday     Memorial        Labor     Columbus 
    20140901     20141013     20141111     20141127     20141225     20150101     20150119     20150216     20150525     20150907     20151012 
    Veterans Thanksgiving    Christmas     NewYears       MLKing   GWBirthday     Memorial Independence        Labor     Columbus     Veterans 
    20151111     20151126     20151225     20160101     20160118     20160215     20160530     20160704     20160905     20161010     20161111 
Thanksgiving    Christmas     NewYears       MLKing   GWBirthday     Memorial Independence        Labor     Columbus Thanksgiving    Christmas 
    20161124     20161226     20170102     20170116     20170220     20170529     20170704     20170904     20171009     20171123     20171225 
    NewYears       MLKing   GWBirthday     Memorial Independence        Labor     Columbus     Veterans Thanksgiving    Christmas     NewYears 
    20180101     20180115     20180219     20180528     20180704     20180903     20181008     20181112     20181122     20181225     20190101 
      MLKing   GWBirthday     Memorial Independence        Labor     Columbus     Veterans Thanksgiving    Christmas     NewYears       MLKing 
    20190121     20190218     20190527     20190704     20190902     20191014     20191111     20191128     20191225     20200101     20200120 
  GWBirthday     Memorial        Labor     Columbus     Veterans Thanksgiving    Christmas     NewYears       MLKing   GWBirthday     Memorial 
    20200217     20200525     20200907     20201012     20201111     20201126     20201225     20210101     20210118     20210215     20210531 
Independence        Labor     Columbus     Veterans Thanksgiving       MLKing   GWBirthday     Memorial   Juneteenth Independence        Labor 
    20210705     20210906     20211011     20211111     20211125     20220117     20220221     20220530     20220620     20220704     20220905 
    Columbus     Veterans Thanksgiving    Christmas 
    20221010     20221111     20221124     20221226 

而预期的df 是这样的:

expected_df 

>>
holiday_name     date
2010-01-01       NewYears
2010-01-18       MLKing
2010-05-31       GWBirthday
...       

如何在 R 中以最佳方式做到这一点?

【问题讨论】:

    标签: r dataframe date time-series holidays-gem


    【解决方案1】:

    我们可以将 stack 它添加到两列 data.frame 中,因为它是 named 向量

    > str(hld)
     Named num [1:114] 20100101 20100118 20100215 20100531 20100705 ...
     - attr(*, "names")= chr [1:114] "NewYears" "MLKing" "GWBirthday" "Memorial" ...
    
    hld2 <- stack(hld)
    names(hld2) <- c("holiday_name", "date")
    hld2$holiday_name <- as.Date(as.character(hld2$holiday_name), "%Y%m%d")
    

    -输出

    > head(hld2)
      holiday_name         date
    1   2010-01-01     NewYears
    2   2010-01-18       MLKing
    3   2010-02-15   GWBirthday
    4   2010-05-31     Memorial
    5   2010-07-05 Independence
    6   2010-09-06        Labor
    

    【讨论】:

    • 伟大的@akrun!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-04
    • 1970-01-01
    • 2021-12-24
    • 2022-12-13
    • 2014-12-31
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多