Is there a standard/common method/formula to calculate the number of months between two dates in R?
I am looking for something that is similar to MathWorks months function
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I was about to say that’s simple, but
difftime()stops at weeks. How odd.So one possible answer would be to hack something up:
Seems about right. One could wrap this into some simple class structure. Or leave it as a hack 🙂
Edit: Also seems to work with your examples from the Mathworks:
Adding the
EndOfMonthflag is left as an exercise to the reader 🙂Edit 2: Maybe
difftimeleaves it out as there is no reliable way to express fractional difference which would be consistent with thedifftimebehavior for other units.