如何计算两个给定日期(基线和后续)之间的月数

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

我正在尝试确定基线和跟进之间的月数 我的约会对象是这样的

-------------------------
|  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   |
--------------------------

我正在寻找这样的输出

---------------------------------------------
|  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              |
---------------------------------------------

我想知道我可以使用哪个包来做这个计算

谢谢

这里有一个选项lubridate

library(dplyr)
library(lubridate)
df1 %>%
      mutate(Months_difference = (interval(mdy(Baseline), 
             mdy(Follow_Up))) %/% months(1))

使用base R,我们可以将数据转换为"Date"对象,然后使用difftime计算两列之间的差异。

df[] <- lapply(df, as.Date, "%m/%d/%y")
df$Month_diff <- as.numeric(difftime(df$Follow_Up, df$Baseline, units = "days")/30)
df

#    Baseline  Follow_Up Month_diff
#1 2015-10-06 2017-10-10       24.5
#2 2015-10-06 2018-04-20       30.9
#3 2015-10-06 2018-04-18       30.8
#4 2015-10-06 2018-07-02       33.3
#5 2015-10-06 2017-08-08       22.4
#6 2015-10-06 2018-01-17       27.8
#7 2015-10-06 2017-10-19       24.8

数据

df <- structure(list(Baseline = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L), .Label = "10/6/15", class = "factor"), Follow_Up = structure(c(2L, 
5L, 4L, 6L, 7L, 1L, 3L), .Label = c("1/17/18", "10/10/17", "10/19/17", 
"4/18/18", "4/20/18", "7/2/18", "8/8/17"), class = "factor")), 
class = "data.frame", row.names = c(NA, -7L))