Man, working with dates is hard!!
I have the following data, and am trying to apply Tidyverse principles and using lubridate for the date components
dates <- data.frame(date = as.Date(c('2017-12-17',
'2017-12-28',
'2018-01-03',
'2018-01-19')))
I want to define the year, and the week of the year for each of the dates, this is simple enough.
new <- dates %>%
mutate(c_year = year(date),
c_week = week(date))
What I really want to know is the exact date (ymd) for the beginning of the 'c_week' for each 'c_year'. However, by the time I want to calculate the beginning of 'c_week' I do not have the original full date to use floor_date(). So I was poking around the internet and found this little bit of code that works (mostly) fine that calculates the new 's_create_wk' variable, and btw I want Mondays to be the start of the week:
new <- dates %>%
mutate(c_year = year(date),
c_week = week(date),
s_create_wk = as.Date(paste0(c_year,c_week, "1"),
"%Y%W%u"))
So theoretically I should get:
date c_year c_week start_of_wk
1 2017-12-17 2017 51 2017-12-11
2 2017-12-28 2017 53 2017-12-25
3 2018-01-03 2018 1 2018-01-01
4 2018-01-19 2018 3 2018-01-15
But what I am actually getting is this:
date c_year c_week start_of_wk
1 2017-12-17 2017 51 2017-12-18
2 2017-12-28 2017 52 2017-12-25
3 2018-01-03 2018 1 NA
4 2018-01-19 2018 3 NA
Looking at my Outlook calendar, 2017-12-25 should be in week 53, but whether I use 'week()' or 'isoweek()' I get an actual as shown. Which is confusing because week 51 is correct for the original date of 2017-12-17. Additionally, 'start_of_wk' is calculating one week late for 'c_week' - this is way confusing. If I subtract off 7 days I get the right 'start-of-wk' for 'c_week', but this seems like the wrong thing to be doing.
To make a long story short and the biggest problem is that I'm getting NAs for 'start_of_wk' for the 2018 dates and I cannot figure out why!!
Sorry if this is confusing, but this is certainly taking me for a spin. I'm guessing that either the formatting for the date is wrong calculating 's_create_wk', but I have tried many combination (Uu, Vv, Ww), or maybe I'm using the wrong package or the wrong functions to work the date components correctly.
Thanks for the help.