Issue with converting calendar week into date for CW01 -2024

Hello,
I have an issue to get the the date from a field like 'YYYY - CWxx' for the first week of 2024. I see that 2023 has only 52 weeks and I'm guessing that is creating the issue.
If you can see from the results of my code on the calendar week '2024 - CW01' the date assigned is '2024-01-08'; I would expect and I need instead the date '2024-01-01'.
Below my code:

sample <- data.frame(service_type = c("A","B","C","A","B","C","A","B","C"),
                     qty = c(38,185,87,29,12,133,2,14,31),
                     dat_cal_week_id = c("202352","202352","202352","202401","202401","202401","202402","202402","202402"),
                     week = c("CW52","CW52","CW52","CW01","CW01","CW01","CW02","CW02","CW02")
)


sample$year_week <- paste(substr(as.character(sample$dat_cal_week_id),1,4) ,'-', sample$week)
sample <- data.frame(service_type = c("A","B","C","A","B","C","A","B","C"),
                     qty = c(38,185,87,29,12,133,2,14,31),
                     dat_cal_week_id = c("202352","202352","202352","202401","202401","202401","202402","202402","202402"),
                     week = c("CW52","CW52","CW52","CW01","CW01","CW01","CW02","CW02","CW02")
)


sample$year_week <- paste(substr(as.character(sample$dat_cal_week_id),1,4) ,'-', sample$week)

I might use, for example:


library(ISOweek)
data.frame(dat_cal_week_id = c("202352","202352","202352","202401","202401","202401","202402","202402","202402")
) |> dplyr::mutate(
  isoweek = paste(substr(dat_cal_week_id,1,4),"-W",substr(dat_cal_week_id,5,6),"-1",sep=""),
  date = ISOweek2date(isoweek) 
  )
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.