[R] Unintended behaviour of stats::time not returning integers for the first cycle
Andreï V. Kostyrka
@ndre|@ko@tyrk@ @end|ng |rom gm@||@com
Fri Oct 14 13:07:16 CEST 2022
Dear all,
I was using stats::time to obtain the year as a floor of it, and
encountered a problem: due to a rounding error (most likely due to its
reliance on the base::seq.int internally, but correct me if I am wrong),
the actual number corresponding to the beginning of a year X can still be
(X-1).9999999..., resulting in the following undesirable behaviour.
One of the simplest ways of getting the year from a ts object is
floor(time(...)). However, if the starting time cannot be represented
nicely as a power of 2, then, supposedly integer time does not have a
.000000... mantissa:
x <- ts(2:252, start = c(2002, 2), freq = 12)
d <- seq.Date(as.Date("2002-02-01"), to = as.Date("2022-12-01"), by =
"month")
true.year <- rep(2002:2022, each = 12)[-1]
wrong.year <- floor(as.numeric(time(x)))
tail(cbind(as.character(d), true.year, wrong.year), 15) # Look at 2022-01-01
print(as.numeric(time(x))[240], 20) # 2021.9999999999997726, the floor of
which is 2021
Yes, I have read the 'R inferno' book and know the famous '0.3 != 0.7 -
0.4' example, but I believe that the expected / intended behaviour would be
actually returning round years for the first observation in a year. This
could be achieved by rounding the near-integer time to integers.
Since users working with dates are expecting to get exact integer years for
the first cycle of a ts, this should be changed. Thank you in advance for
considering a fix.
Yours sincerely,
Andreï V. Kostyrka
[[alternative HTML version deleted]]
More information about the R-help
mailing list