Count the months between two dates in a data.table

喜夏-厌秋 提交于 2020-07-19 18:24:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!