Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Transform year/week to date object

Tags:

date

r

lubridate

String contains 'YEAR WEEK' and I want to transform it with parse_date_time() to a date object but I can't make the code work:

parse_date_time(c("201510"), "YW")

I don't have to use lubridate, can be other packages, too.

like image 488
Sebastian Avatar asked Aug 07 '17 14:08

Sebastian


2 Answers

Before converting year-week to a date you have to specify a day of the week but more importantly you have to ensure which of the different conventions is being used.

Base R's strptime() function knows 3 definitions of week of the year (but supports only 2 of them on input) and 2 definitions of weekday number, see ?strptime:

Week of the year

  • US convention: 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): %U

  • UK convention: Week of the year as decimal number (00–53) using Monday as the first day of week (and typically with the first Monday of the year as day 1 of week 1): %W

  • ISO 8601 definition: Week of the year as decimal number (01–53) as defined in ISO 8601. If the week (starting on Monday) containing 1 January has four or more days in the new year, then it is considered week 1. Otherwise, it is the last week of the previous year, and the next week is week 1: %V which is accepted but ignored on input.
    Note that there is also a week-based year (%G and %g) which is to be used with %V as it may differ from the calendar year (%Y and %y).

Numeric weekday

  • Weekday as a decimal number (1–7, Monday is 1): %u
  • Weekday as decimal number (0–6, Sunday is 0): %w
  • Interestingly, there is no format for the case Sunday is counted as day 1 of the week.

Converting year-week-day with the different conventions

If we append day 1 to the string and use the different formats we do get

as.Date("2015101", "%Y%U%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%U%w")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%u")
# [1] "2015-03-09"
as.Date("2015101", "%Y%W%w")
# [1] "2015-03-09"
as.Date("2015101", "%G%V%u")
# [1] NA

For weekday formats %u and %w we do get the same result because day 1 is Monday in both conventions (but watch out when dealing with Sundays).

For 2015, the US and the UK definition for week of the year coincide but this is not true for all years, e.g., not for 2001, 2007, and 2018:

as.Date("2018101", "%Y%U%u")
#[1] "2018-03-12"
as.Date("2018101", "%Y%W%u")
#[1] "2018-03-05"

The ISO 8601 format specifiers aren't supported on input. Therefore, I had created the ISOweek package some years ago:

ISOweek::ISOweek2date("2015-W10-1")
#[1] "2015-03-02"

Edit: Using Thursday to associate a week with a month

As mentioned above you need to specify a day of the week to get a full calendar date. This is also required if the dates need to be aggregated by month later on.

If no weekday is specified and if the dates are supposed to be aggregated by month later on, you may take the Thursday of each week as reference day (following a suggestion by djhurio). This ensures that the whole week is assigned to the month to which the majority of the days of the week belong to.

For example, taking Sunday as reference day would return

ISOweek::ISOweek2date("2015-W09-7")
[1] "2015-03-01"

which consequently would associate the whole week to the month of March although only one day of the week belongs to March while the other 6 days belong to February. Taking Thursday as reference day will return a date in February:

ISOweek::ISOweek2date("2015-W09-4")
[1] "2015-02-26"
like image 124
Uwe Avatar answered Oct 27 '22 00:10

Uwe


Yes ISOweek package does this

ISOweek::ISOweek2date(isoWeek)

but for the other direction, check up the newer lubridate package as well

ISOweek::date2ISOweek(yourDate)

lubridate::isoweek(ymd(yourDate))
like image 20
hhh Avatar answered Oct 26 '22 23:10

hhh