R – Convert week number to date

as.dater

I have a data frame in R with the week of the year that I would like to convert to a date. I know I have to pick a year and a day of the week so I am fixing those values at 2014 and 1. Converting this to a date seems simple:

as.Date(paste(2014,df$Week,1,sep=""),"%Y%U%u")

But this code only works if week is greater than 9. Week 1 – 9 returns NA. If I change the week to 01,02,03… it still returns NA.

Anyone see what I am missing?

Best Answer

as.Date is calling the 1 to 9 as NA as it is expects two digits for the week number and can't properly parse it.

To fix it, add in some - to split things up:

as.Date(paste(2014, df$Week, 1, sep="-"), "%Y-%U-%u")
Related Topic