我正在尝试选择数据框的子集,其中日期需要小于(计算/变量)日期。
以下代码会引发错误:
loanFrame_excluding_young <- loanFrame[loanFrame$LoanEffective < AddMonths(as.Date("2015-11-11"),-loanFrame$TermMonths),]
Error in seq.Date(X[[i]], ...) : 'by' must be of length 1
有什么想法吗?
答案 0 :(得分:0)
问题在于DescTools :: AddMonths函数。在AddMonths(x,n,ceiling = TRUE)中,n只能是单个数字,而不是矢量。
使用以下代码可以使用lubridate的%m-%函数。
library(lubridate)
loanFrame <- data.frame(TermMonths = c(1,3,5,7),
LoanEffective = as.Date(c("2015-09-15", "2015-08-05", "2015-10-01", "2015-06-25")))
loanFrame_excluding_young <- loanFrame[loanFrame$LoanEffective < as.Date("2015-11-11") %m-% months(loanFrame$TermMonths),]
loanFrame_excluding_young
TermMonths LoanEffective
1 1 2015-09-15
2 3 2015-08-05