Calculate a weeks start date given week number and year

Hi Everyone,
I want to calculate a weeks start date given week number and year.

For that, I tried to use as.Date function and formatting stated below. But the same function is not working or it is returning NA when week number is 53.

+         format = "%Y-%U-%u")
[1] "2020-12-28"

> as.Date(x = "2020-53-1",
+         format = "%Y-%U-%u")
[1] NA```.   It seems it is not working for week number 53/ or it is giving output a week ahead.

From the strptime docs (which you can access using ?strptime)

`%U`

Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week (and typically with the first Sunday of the year as day 1 of week 1). The US convention.

I think the issue is that it is trying to return the Sunday of week 53, which is invalid (see the reprex of your code with the full error message below)

as.Date(x = "2020-53-1",
        format = "%Y-%U-%u")
#> Warning in strptime(x, format, tz = "GMT"): (0-based) yday 369 in year 2020 is
#> invalid
#> [1] NA

Created on 2021-01-05 by the reprex package (v0.3.0.9001)

Thanks, Actually the concern is around week 53, Where I am getting NA.

Right. That's the result of the code in the reprex as well, but if you read the warning message, and the documentation for as.Date() (which uses strptime()), it explains what the problem is when running the code in base R.

What can be the better solution to get a startday of week, given week 53 as week number and dynamic years maybe 2020 or 2016 etc.,?

How are you defining weeks? There are generally 3 possible ways, and knowing which one you mean is important to this question:

  • The ISO week convention
    • Weeks start with Monday. Each week's year is the Gregorian year in which the Thursday falls. The first week of the year, hence, always contains January 4th
  • The US week convention
    • Weeks start with Sunday. Week 1 starts with the first Sunday in that year. Days before that are considered week 0 of that year.
  • The UK week convention
    • Weeks start with Monday. Week 1 starts with the first Monday in that year. Days before that are considered week 0 of that year.
1 Like

I am just trying to get the start date of week, it can give me any day as an output(considering any day as start of week Monday/Sunday/Tuesday etc.,), just that I want to handle week number 53 and year, for example 2020. I get data in format W53-2020, and I am trying to get start week of date using its week number and year.

library(tidyverse)
library(lubridate)

(a <- tibble(d1 = "W53-2020"))
(b <- mutate(rowwise(a),
             d2 = str_split(d1,"-"),
             year=d2[[2]],
             weeknum=parse_integer(str_remove(d2[[1]],"W")),
             date = ymd(paste0(year,"-01-01")) + weeks(weeknum))) 
# A tibble: 1 x 5
# Rowwise: 
  d1       d2        year  weeknum date      
  <chr>    <list>    <chr>   <int> <date>    
1 W53-2020 <chr [2]> 2020       53 2021-01-06
1 Like

This topic was automatically closed 21 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.