0

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
nak5120
  • 4,089
  • 4
  • 35
  • 94
  • 1
    What if the week spans two months? Do you want the first day of the week of that number, where the week starts on the first day of the year? Or does the week always start on Sunday or Monday? – Spacedman Oct 17 '18 at 20:35
  • 1
    How do you intend to handle weeks which covers two months? What have you tried? – Henrik Oct 17 '18 at 20:35
  • If it crosses, two months then I would take the 2nd month. Thanks – nak5120 Oct 17 '18 at 20:39

1 Answers1

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))
Jordo82
  • 796
  • 4
  • 14