Jiří Moravec
2024-Feb-06 21:23 UTC
[Rd] round.Date and trunc.Date not working / implemented
This is my first time working with dates, so if the answer is "Duh, work with POSIXt", please ignore it. Why is not `round.Date` and `trunc.Date` "implemented" for `Date`? Is this because `Date` is (mostly) a virtual class setup for a better inheritance or is that something that is just missing? (like `sort.data.frame`). Would R core welcome a patch? I decided to convert some dates to date using `as.Date` function, which converts to a plain `Date` class, because that felt natural. But then when trying to round to closest year, I have realized that the `round` and `trunc` for `Date` do not behave as for `POSIXt`. I would assume that these will have equivalent output: Sys.time() |> round("years") # 2024-01-01 NZDT Sys.Date() |> round("years") # Error in round.default(...): non-numeric argument to mathematical function Looking at the code (and reading the documentation more carefully) shows the issue, but this looks like an omission that should be patched. -- Jirka
Martin Maechler
2024-Feb-08 14:15 UTC
[Rd] round.Date and trunc.Date not working / implemented
>>>>> Ji?? Moravec >>>>> on Wed, 7 Feb 2024 10:23:15 +1300 writes:> This is my first time working with dates, so if the answer is "Duh, work > with POSIXt", please ignore it. > Why is not `round.Date` and `trunc.Date` "implemented" for `Date`? > Is this because `Date` is (mostly) a virtual class setup for a better > inheritance or is that something that is just missing? (like > `sort.data.frame`). Would R core welcome a patch? > I decided to convert some dates to date using `as.Date` function, which > converts to a plain `Date` class, because that felt natural. > But then when trying to round to closest year, I have realized that the > `round` and `trunc` for `Date` do not behave as for `POSIXt`. > I would assume that these will have equivalent output: > Sys.time() |> round("years") # 2024-01-01 NZDT > Sys.Date() |> round("years") # Error in round.default(...): non-numeric > argument to mathematical function > Looking at the code (and reading the documentation more carefully) shows > the issue, but this looks like an omission that should be patched. > -- Jirka You are wrong: They *are* implemented, both even visible since they are in the 'base' package! ==> they have help pages you can read .... Here are examples:> trunc(Sys.Date())[1] "2024-02-08"> trunc(Sys.Date(), "month")[1] "2024-02-01"> trunc(Sys.Date(), "year")[1] "2024-01-01">
Possibly Parallel Threads
- round.Date and trunc.Date not working / implemented
- round.Date and trunc.Date not working / implemented
- round.Date and trunc.Date not working / implemented
- round.Date and trunc.Date not working / implemented
- round.Date and trunc.Date not working / implemented