从周和年中提取月份

问题描述:

我有一个数据框,其中使用lubridate提取了星期和年份.我没有日期值了.

I have a dataframe where I extracted the week and year number using lubridate. I do not have the date values anymore.

我想从星期数和年数中提取月份.

I want to extract the month from the week number and year number.

df
Week    Year  
   1    2018
   5    2018
  45    2017

我希望我的最终输出是这样:

I want my final output to be this:

Week    Year  Month
   1    2018      1
   5    2018      2
  45    2017     11

这将提取一周中第一天的月份,如果需要,则可以添加一个常量或其他内容(如果需要的话是一周中的第二天或第三天.

This extracts the month of the first day of the week, you could add a constant or something if you wanted the 2nd or 3rd day of the week instead.

df %>% 
  mutate(Month = month(ymd(Year * 10000 + 0101) + Week * 7))