Search code examples
rdatelubridateposixct

Get days based on integer week and year in R


I have many strings like "200046". The first four digits are the year, and the last two is the number of the week per year. I'm trying to find the 7 days of the week for that week. I tried something like

date = as.Date(str, "%Y%M")

but it returns "2000-01-29" which is not the 46th week of 2000. How can I do that?


Solution

  • Add day of the week to str.

    str <- '200046'
    as.Date(paste0(str, 1), "%Y%U%u")
    #[1] "2000-11-13"
    

    This is 1st day (Monday) of 46th week of 2000.

    Now to get all days of the week you can do :

    as.Date(paste0(str, 1), "%Y%U%u") + 0:6
    #[1] "2000-11-13" "2000-11-14" "2000-11-15" "2000-11-16" "2000-11-17" "2000-11-18" "2000-11-19"