6

I'm looking for a function that will get year + week number + Week day, and return a date, for example:

I would like to input the 3 following

 2015
 Monday
 23

And get the desired output:

"2015-06-08"

After Searching in the Web, there seems to be equivalent question in other languages but not in R:

How to Get date from week number and year

Any help on that would be great!

Community
  • 1
  • 1
Yehoshaphat Schellekens
  • 2,305
  • 2
  • 22
  • 49

2 Answers2

11

Using strptime:

strptime("2015Monday23", "%Y%A%U")
# [1] "2015-06-08"

Or more generally

strptime(paste0(2015, "Monday", 23), "%Y%A%U")
David Arenburg
  • 91,361
  • 17
  • 137
  • 196
  • 2
    `strptime` is returning the same classes/attributes as `as.POSIXlt`. Though I forgot about `A`, so nice one there. – David Arenburg Jun 11 '15 at 09:40
  • Be careful with ISO dates where the isoyear != year (can occur on first / last week of the year, see https://en.wikipedia.org/wiki/ISO_week_date#First_week) – koldLight Jun 09 '20 at 13:41
1

There are two caveats here:

  1. The result depends on the current locale.
    In my locale "German_Germany.1252" (call Sys.getlocale("LC_TIME") to check your locale), strptime("2015Monday23", "%Y%A%U") returns NA.

  2. The results depends on the convention for numbering the weeks of a year.
    There are 3 conventions R is aware of: US, UK, and ISO 8601. See this answer for a detailed discussion. So, the convention to be used for conversion has to be specified.

Non-English locales

If you are in a non-english locale, you can deal with English weekday names (or month names, likewise) by temporarily changing the current locale:

Sys.setlocale("LC_TIME", "US")
#> [1] "English_United States.1252"
strptime("2015Monday23", "%Y%A%U")
#> [1] "2015-06-08 CEST"
Sys.setlocale("LC_TIME")
#> [1] "German_Germany.1252"

The lubridate package offers a more convenient way:

lubridate::parse_date_time("2015Monday23", "YAU", locale = "US")
#> [1] "2015-06-08 UTC"

Week of the year in different conventions

As the weekday is given by its name, the US and UK conventions return the same result:

lubridate::parse_date_time("2015Monday23", "YAU", locale = "US")
#> [1] "2015-06-08 UTC"
lubridate::parse_date_time("2015Monday23", "YAW", locale = "UK")
#> [1] "2015-06-08 UTC"

Unfortunately, the format specifiers for the ISO 8601 convention are not accepted on input. So, we reverse the process and format the resulting date as week of the year in the different conventions which shows a differing result for ISO 8601.

format(as.Date("2015-06-08 UTC"), "%Y-%W-%u") # UK convention
#> [1] "2015-23-1"
format(as.Date("2015-06-08 UTC"), "%Y-%U-%w") # US convention
#> [1] "2015-23-1"
format(as.Date("2015-06-08 UTC"), "%G-%V-%u") # ISO 8601
#> [1] "2015-24-1"
Uwe
  • 41,420
  • 11
  • 90
  • 134