问题
I have a data.table
like the following:
ID start_date end_date
1 2015.01.01 2016.02.01
2 2015.06.01 2016.03.01
3 2016.01.01 2017.01.01
I would like to get the following:
ID start_date end_date Months_passed
1 2015.01.01 2016.02.01 13
2 2015.06.01 2016.03.01 9
3 2016.01.01 2017.01.01 12
I was trying the following code:
DT[, Months_passed:= length(seq(from = start_date, to = end_date, by='month')) - 1]
but I get the error, that
"Error in seq.Date(from = start_date, to = end_date, by = "month") : 'from' must be of length 1"
回答1:
Here's a possible approach using data.table. First, turn your dates to real date-format:
df[, 2:3 := lapply(.SD, as.IDate, format = "%Y.%m.%d"), .SDcols = 2:3]
Then, get the months that passed:
df[, months_passed := lengths(Map(seq, start_date, end_date, by = "months")) -1]
So basically you need to Map
the start and end dates to seq
.
The result is:
df
# ID start_date end_date months_passed
#1: 1 2015-01-01 2016-02-01 13
#2: 2 2015-06-01 2016-03-01 9
#3: 3 2016-01-01 2017-01-01 12
来源:https://stackoverflow.com/questions/45184782/count-the-months-between-two-dates-in-a-data-table