3

I'd like to calculate a difference between two dates in years with decimal values. Let's say we have "1978-08-25" and "2014-02-05" (%Y-%m-%d). How can I calculate the difference between these two dates in years with decimal values (ie. not just 35 years, but 35.95...years)? Thank you in advance.

midtownguru
  • 2,583
  • 5
  • 22
  • 24

2 Answers2

3

You could check out the zoo package.

x = as.yearmon("2014-02-05")
y = as.yearmon("1978-08-25")

x - y
[1] 35.5
AGS
  • 14,288
  • 5
  • 52
  • 67
1

It's probably worth noting that this really can't be answered definitively (as @hadley alluded to). Even the base difftime function in R only allows the options of:

...units = c("auto", "secs", "mins", "hours", "days", "weeks"))

No months, no years, because they are not definite values. Having said that, you can closely approximate something sensible using @flodel's method in the comments.

thelatemail
  • 91,185
  • 12
  • 128
  • 188