Extract Month from Week and Year number [duplicate

2020-05-01 14:42发布

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

标签: r date lubridate
1条回答
爱情/是我丢掉的垃圾
2楼-- · 2020-05-01 15:21

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))
查看更多
登录 后发表回答