0

I thought I had understood how I can use as.Date() but I get an unexpected result when using the UK convention for counting weeks in a given year (%W).

I have a year-week combination and want to find the date corresponding to Monday and Sunday of that week (if they exist, so it's fine that NA is returned if the week does not contain Mon or Sun). In the UK week counting, the week starts with a Monday. So to find the Monday of, say, week 1 in 2016, I use the following code, which returns the correct result (4 Jan 2016):

as.Date("2016011", format = "%Y%W%u")

To find the Sunday of that week, I change the last number to 7 because %u takes 1 to be Monday and 7 to be Sunday (I have also used %w instead with its definition of Sunday 0 and Monday 1 but with the same result):

as.Date("2016017", format = "%Y%W%u")

My expected output is 10 Jan 2016 but I get 3 Jan 2016. So it seems that as.Date() treats the week as beginning with Sunday. This however contradicts the definition of %W.

Any ideas what I'm missing? Thanks!

xb5wz
  • 1
  • 1
  • 1
    If you want Sunday to be the first day of the week, use `%U`. From the documentation of `?strptime`: *"%U Week of the year as decimal number (00–53) using Sunday as the first day 1 of the week"*. And `as.Date("2016017", format = "%Y%U%u")` is the same date as `as.Date("2016010", format = "%Y%W%w")`. Both `"2016-01-03"`. – Rui Barradas Sep 22 '21 at 19:14
  • 1
    This link may help you: https://stackoverflow.com/questions/45549449/transform-year-week-to-date-object – Tom Sep 22 '21 at 19:16
  • Try `as.Date("2016027", format = "%Y%U%u")`. – Rui Barradas Sep 22 '21 at 19:16
  • @RuiBarradas I don't want Sunday to be the first day of the week. The data I am using has chosen the UK convention, i.e. Monday as the first day of the week. Thank you for the suggestion of how to obtain the correct days. I have used a similar workaround (using the Sunday from next week) but my question was more about why the function does not behave as I understood it. – xb5wz Sep 22 '21 at 19:28
  • @Tom Thanks, I had read the post and the answers before. I believe my expectation of what the function should return is consistent with their explanations but the function returns something different. – xb5wz Sep 22 '21 at 19:30

1 Answers1

0

For working with dates, I recommend the lubridate package. Below are some examples.

library(lubridate)

date1 = ymd("20160103")
weekdays(date1)  #Local name of the day of the week
#[1] "niedziela"

wday(date1)      #Weekday number regional setting
#[1] 1

wday(date1, week_start = 1) #Weekday number where Monday is 1 day
#[1] 7

You can set lubridate.week.start option to control this parameter globally.

Here's a bit more information.

Marek Fiołka
  • 4,825
  • 1
  • 5
  • 20
  • Also `isoweek()` and `isoyear()` from the lubridate package for working with ISO8601 dates. I've not yet found a function to convert from iso week to a date (eg. monday of the iso week) in the lubridate package. – Tom Sep 22 '21 at 20:21