Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the lubridate note "method with signature ‘Timespan#Timespan’ chosen for function ‘%/%’" mean?

Tags:

r

lubridate

When I run the following code in R I get a strange note (it only appears the first time I run the code in a session):

> library(lubridate)
Attaching package: ‘lubridate’
The following object is masked from ‘package:base’:
    date
Warning message:
package ‘lubridate’ was built under R version 3.3.2 
> data.frame(i = interval(ymd(20140101), ymd(20160101)))$i %/% years(1)
Note: method with signature ‘Timespan#Timespan’ chosen for function ‘%/%’,
  target signature ‘Interval#Period’.
  "Interval#ANY", "ANY#Period" would also be valid
[1] 2

I am doubly confused:

  1. I am unclear as to what the alternative syntax is that it is recommending. A # is a comment in R, so presumably the hash is meant to mean something other than a hash, but what?
  2. Is it telling me I am doing something wrong? The note seems to suggest it is an FYI, but an FYI is an odd thing to be spat out of a function if there is no problem.
like image 909
Tim Avatar asked Mar 07 '17 05:03

Tim


1 Answers

This warning will only occur the first time you run it to remind you of that doing integer division has the problem that months or years do not necessarily have the same length in other units like hours or days.

Suppose that we divide the interval 2014--2018 by 2 years, it would not be completely correct to say that the answer is 4 because 2016 is a leap year and has 366 days. So it will be correct if you unit of measure is only years, but it is not strictly correct if you present it as an interval (which may be expressed in years, but also in days, or hours).

There is really no way around the warning either (at least not for integer division), as the warning is always to the point, even if you are dividing interval %/% interval or period %/% period.

But it will only show the first time you run your division, after that it goes silent.

data.frame(i = interval(ymd(20140101), ymd(20160101)))$i %/% years(1)
Note: method with signature ‘Timespan#Timespan’ chosen for function ‘%/%’,
 target signature ‘Interval#Period’.
 "Interval#ANY", "ANY#Period" would also be valid
[1] 2
data.frame(i = interval(ymd(20140101), ymd(20160101)))$i %/% years(1)
[1] 2

In theory it should be possible to avoid the warning if both sides of the division are represented by as a timespan class. But I have never tried to do that.

like image 102
FvD Avatar answered Nov 16 '22 21:11

FvD