Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Year fractions using Actual/365 convention in R

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.

like image 766
Mike M Avatar asked Sep 28 '14 09:09

Mike M


1 Answers

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
like image 175
Ricardo Saporta Avatar answered Oct 16 '22 02:10

Ricardo Saporta