r中两个日期之间的月数



我是R的新手,不熟悉它。我有以下数据:

Start.Date       
1: 2020-09-15 
2: 2021-08-10 
3: 2015-08-25 
4: 2021-10-11 
5: 2021-08-15 
6: 2018-06-05 
7: 2021-06-10 
8: 2020-05-13 
9: 2021-08-03 
10: 2017-12-25

我想要当前日期和开始日期之间的持续时间(以月为单位(?

我尝试了以下操作,但没有成功:

install.packages("lubridate")
library("lubridate") 
currentDate <- Sys.Date()
StartDate <- as.Date(Mall_Data$Start.Date, format =  "%m/%d/%Y")
theduration<- interval(currentDate, StartDate) %% months(1)

您的代码中有一些拼写错误和错误的格式。您的日期格式为false,%%不起作用,但%/%会起作用。在此处更正:

library(lubridate)
### worked with
Start.Date <- c("2020-09-15","2021-08-10","2015-08-25","2021-10-11","2021-08-15","2018-06-05","2021-06-10","2020-05-13","2021-08-03","2017-12-25")
### your corrected code
currentDate <- Sys.Date()
StartDate <- as.Date(Start.Date, format =  "%Y-%m-%d")
theduration <- interval(currentDate, StartDate) %/% months(1)
theduration

输出为:

[1] -18  -7 -78  -5  -7 -45  -9 -22  -7 -50

我也会提到两个日期之间的月数

相关内容

  • 没有找到相关文章

最新更新