Extract Month from Week and Year number [duplicate]

只谈情不闲聊 提交于 2019-12-02 18:52:02

问题


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

回答1:


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))


来源:https://stackoverflow.com/questions/52863140/extract-month-from-week-and-year-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!