In R, I have a dataset with the week number and the year (see below) and I want to transform it into the date the corresponding Monday of the week.I used the as.Date()
function. This works well, except for the first week, where the function return NA as the corresponding Monday of the first week belongs to the previous year. I thus want that the function returns the date of Monday, even if it is not the same year. Any idea?
data.frame(week = paste(2022,0:5,sep="-")) %>%
mutate(week2 = paste(week,"1",sep="-"),
date=as.Date(week2, "%Y-%W-%w"))
week week2 date 1 2022-0 2022-0-1 <NA> 2 2022-1 2022-1-1 2022-01-03 3 2022-2 2022-2-1 2022-01-10 4 2022-3 2022-3-1 2022-01-17 5 2022-4 2022-4-1 2022-01-24 6 2022-5 2022-5-1 2022-01-31