This is tricky. There are two problems:
- date/date-time conversion routines don't have a concept of "year/week only" dates. So you need to append a specified day-of-week (e.g. 0 or 1 for the first day of the week, depending on the convention) to get a date.
- the system libraries for date conversion that R uses (it doesn't typically use its own, but wraps system libraries) don't generally have the ISO week convention encoded (see the details of
?strptime
, especially under the description of the "%V" format, where it says
"Accepted but ignored on input" — i.e. it just doesn't work. Therefore, I used the stringi
package, which uses the independent ICU implementation of date conversions. (It does give a warning about "Formatters %U, %V, %x, %X, %u, %w, %r, %g, %G, %c might not be 100% compatible with ICU", so you should definitely check your results!)
This seems to be a solution, although you should check the beginning-of-week dates yourself and possibly subtract 1 from them, if you're getting Mondays but want Sundays (I haven't checked). (I initially tried adding "-0" to the strings, which is good for R, but the stringi
function doesn't like it.)
library(stringi)
w <- vaccine_data$YearWeekISO
sfmt <- stri_datetime_fstr('%Y-W%V-%w')
d <- as.Date(stri_datetime_parse(
sprintf("%s-1",w), format = sfmt))
Previous attempt in R, which gives NA
values for "2020-W53" (because I was using "%W", the UK convention, rather than the non-working "%V" format)
s <- sprintf("%s-0",w)
d <- strptime(s, format="%Y-W%W-%w")
unique(w[which(is.na(d))])
## [1] "2020-W53"
You could replace NA values with your desired date.