我有一个数据框,看起来是:
loan id borrowing_date principal maturity_date maturity_time(years)
01 2013-03-03 1000000 2015-03-03 02
02 2015-07-30 2000000 2017-07-29 02
03 2017-10-03 3000000 2020-10-02 03
我希望每个借贷日期和到期日期都可以在接下来的10年中每季度扩展一次。即应该看起来像
loan id borrowing_date principal maturity_date maturity_time(years)
01 2013-03-03 1000000 2015-03-03 02
01 2013-06-03 1000000 2015-06-03 02
01 2013-09-03 1000000 2015-09-03 02
. . . . .
. . . . .
01 2023-03-03 1000000 2025-03-03 02
. . . . .
. . . . .
03 2017-10-03 3000000 2020-10-02 03
03 2018-01-03 3000000 2021-01-02 03
03 2018-04-03 3000000 2021-04-02 03
. . . . .
. . . . .
03 2027-10-03 3000000 2030-10-02 03
我该怎么做?
答案 0 :(得分:1)
使用data.table
的{{1}}选项
lubridate
说明:我们使用library(data.table)
library(lubridate)
setDT(df)[, .(
borrowing_date = seq(as.Date(borrowing_date), as.Date(borrowing_date) %m+% years(10), by = "quarter"),
principal,
maturity_date = seq(as.Date(maturity_date), as.Date(maturity_date) %m+% years(10), by = "quarter"),
maturity_time.years.),
by = loan.id]
# loan.id borrowing_date principal maturity_date maturity_time.years.
# 1: 1 2013-03-03 1000000 2015-03-03 2
# 2: 1 2013-06-03 1000000 2015-06-03 2
# 3: 1 2013-09-03 1000000 2015-09-03 2
# 4: 1 2013-12-03 1000000 2015-12-03 2
# 5: 1 2014-03-03 1000000 2016-03-03 2
#---
#119: 3 2026-10-03 3000000 2029-10-02 3
#120: 3 2027-01-03 3000000 2030-01-02 3
#121: 3 2027-04-03 3000000 2030-04-02 3
#122: 3 2027-07-03 3000000 2030-07-02 3
#123: 3 2027-10-03 3000000 2030-10-02 3
生成日期序列seq.Date
,其中最终日期是原始日期加上10年(或使用by = "quarter"
语法lubridate
)
请注意,列名已更改(R中的列名通常带有特殊字符(例如空格,逗号等)是不明智的做法。
%m+% years(10)