Is there any function/package that can compute year fraction (differences between two dates) with different day-counting convention, like yearfrac() in Matlab? I need to use Actual/365 convention.
rollYourOwn <- function(D, origin=as.Date("1970-01-01")) {
if (!inherits(D, "Date"))
D <- as.Date(D, origin=origin)
as.numeric(D - as.Date(format(D, "%Y-01-01"), origin=origin) + 1) / 365
}
rollYourOwn("2014-01-01")
# [1] 0.00273973
rollYourOwn(Sys.Date())
# [1] 0.742466
rollYourOwn("2014-12-31")
# [1] 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With