lubridate::isoyear() returns unexpected outputs

Viewed 289

I am using lubridate package to work with dates in R. I had an unexpected return from the isoyear() function.

library(lubridate)      

isoyear("2014-12-31")  
[1] 2015    

year("2014-12-31")  
[1] 2014  

The isoyear() function says 2014-12-31 is in 2015, whereas 2015 says its in 2014. Obviously the return of year() is what I expected.

Could someone explain this difference in behaviour between isoyear and year? I can't think of any case where I would want the return of 2015 for 2014-12-31.

1 Answers

You have to take into account, that the output gives the Gregorian year.

The ISO year number deviates from the number of the Gregorian year on, if applicable, a Friday, Saturday, and Sunday, or a Saturday and Sunday, or just a Sunday, at the start of the Gregorian year (which are at the end of the previous ISO year) and a Monday, Tuesday and Wednesday, or a Monday and Tuesday, or just a Monday, at the end of the Gregorian year (which are in week 01 of the next ISO year).
In the period 4 January–28 December and on all Thursdays the ISO year number is always equal to the Gregorian year number.

See some examples in the period 29th December - 3rd January, where this differs:

library(lubridate)
isoyear(ymd("2005-01-01"))
[1] 2004
isoyear(ymd("2008-12-30"))
[1] 2009

So this is no bug ... it just follows the calculations for iso years.

Related