I am trying to do some date calculations like below:
library(optionstrat) #compute the difference between two dates tdiff("2022-10-20", "2022-11-30", "days") # return with an integer of 41, which is good. tdiff(Sys.Date()-1, "2022-11-30", "days") # return with ***41.66***
Wondering what I did wrong in the third line, and may I ask how do I make it as an integer instead? Thank you so much.
CodePudding user response:
If you supply dates as character
scalars it seems to work
library(opionstrat)
tdiff(format(Sys.Date() - 1, "%Y-%m-%d"), "2022-11-30", "days")
Unfortunately, the documentation for optionstrat::tdiff
is not very detailed. It seems that tdiff
simply calls difftime
under the hood. difftime
in turn needs two time points, either as a POSIXct
/Date
object or as something that is coercible to a POSIXct
/Date
object. It seems that providing time1
and time2
in different formats (one as a Date
object and one as a character
scalar) does not work well.
(Tangential) PS. I don't really see the point of optionstrat::tdiff
, seeing that it's really just difftime
with the two time points swapped.