Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I calculate the number of months between two given dates ( baseline and follow-up)

I'm trying to determine the number of months between baseline and followup my date looks like this

-------------------------
|  Baseline | Follow_Up  |
-------------------------
|  10/6/15  | 10/10/17   |
|  10/6/15  | 4/20/18    |
|  10/6/15  | 4/18/18    |
|  10/6/15  | 7/2/18     |
|  10/6/15  | 8/8/17     |
|  10/6/15  | 1/17/18    |
|  10/6/15  | 10/19/17   |
--------------------------

And I looking for output as this

---------------------------------------------
|  Baseline | Follow_Up  | Months difference|
---------------------------------------------
|  10/6/15  | 10/10/17   |24.5              |
|  10/6/15  | 4/20/18    |30.9              |
|  10/6/15  | 4/18/18    |30.8              |
|  10/6/15  | 7/2/18     |33.3              |
|  10/6/15  | 8/8/17     |22.4              |
|  10/6/15  | 1/17/18    |27.8              | 
|  10/6/15  | 10/19/17   |24.8              |
---------------------------------------------

I would like to know which package can I use to do this calculation

Thank you

like image 902
Mr.M Avatar asked Oct 24 '25 01:10

Mr.M


1 Answers

Here is an option with lubridate

library(dplyr)
library(lubridate)
df1 %>%
      mutate(Months_difference = (interval(mdy(Baseline), 
             mdy(Follow_Up))) %/% months(1))
like image 138
akrun Avatar answered Oct 26 '25 17:10

akrun