Convert YearWeek double to date with lubridate?

Is there a way to convert a number like 201901 (first week of 2019) to a date variable?

Thanks!

library(lubridate)
yw_to_date <- function(yw){
  yearpart <- as.integer(yw/100)
  weekpart <- yw - yearpart*100
  
 new_date <- lubridate::ymd(paste0(yearpart,"01","01"))
 week(new_date) <- weekpart
 new_date
}
(dvec <- 202000+1:52)
 [1] 202001 202002 202003 202004 202005 202006 202007 202008 202009 202010 202011 202012 202013 202014 202015 202016
[17] 202017 202018 202019 202020 202021 202022 202023 202024 202025 202026 202027 202028 202029 202030 202031 202032
[33] 202033 202034 202035 202036 202037 202038 202039 202040 202041 202042 202043 202044 202045 202046 202047 202048
[49] 202049 202050 202051 202052
yw_to_date(dvec)
[1] "2020-01-01" "2020-01-08" "2020-01-15" "2020-01-22" "2020-01-29" "2020-02-05" "2020-02-12" "2020-02-19"
[9] "2020-02-26" "2020-03-04" "2020-03-11" "2020-03-18" "2020-03-25" "2020-04-01" "2020-04-08" "2020-04-15"
[17] "2020-04-22" "2020-04-29" "2020-05-06" "2020-05-13" "2020-05-20" "2020-05-27" "2020-06-03" "2020-06-10"
[25] "2020-06-17" "2020-06-24" "2020-07-01" "2020-07-08" "2020-07-15" "2020-07-22" "2020-07-29" "2020-08-05"
[33] "2020-08-12" "2020-08-19" "2020-08-26" "2020-09-02" "2020-09-09" "2020-09-16" "2020-09-23" "2020-09-30"
[41] "2020-10-07" "2020-10-14" "2020-10-21" "2020-10-28" "2020-11-04" "2020-11-11" "2020-11-18" "2020-11-25"
[49] "2020-12-02" "2020-12-09" "2020-12-16" "2020-12-23"
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.