我有以下数据。
df_a <- data.frame(id=c("John","Ben","Bill", "Eminem"),
amount=c("300", "500", "1000", "1200"),
issue_date=as.Date(c("2010-01-01","2011-01-01","2012-01-01", "2015-02-01")),
last_pymnt_date=as.Date(c("2013-02-01","2012-05-01","2014-01-01", "2018-02-01")),
months_passed=c(37,16,24,36),
term = c("36", "36", "36", "36"),
status=c("Fully Paid",
"Charged off",
"Does not meet the credit policy. Status:Charged Off",
"Does not meet the credit policy. Status:Fully Paid"),
stringsAsFactors = F)
library(DescTools)
df_a$maturity_dt <- AddMonths(df_a$issue_date, 36)
如果last_pymnt_date
变量包含表达式“ Fully Paid”,我想使maturity_dt
与status
相同。如果我运行以下行,它将使last_pymnt_date
变成一些数字。
df_a$last_pymnt_date <- ifelse(grepl("Fully Paid", df_a$status),
df_a$maturity_dt,
df_a$last_pymnt_date)
有什么建议吗?
谢谢
答案 0 :(得分:3)
这是执行此操作的简单方法。刚刚分开进行grepl
测试以提高可读性。
test <- grepl("Fully Paid", df_a$status)
df_a$last_pymnt_date[test] <- df_a$maturity_dt[test]
答案 1 :(得分:2)
使用dplyr
的解决方案:
df_a$last_pymnt_date <- dplyr::if_else(grepl("Fully Paid", df_a$status),
df_a$maturity_dt,
df_a$last_pymnt_date)
dplyr
的{{1}}也是case_when
的不错选择,尤其是在您有更多条件的情况下:
ifelse
或使用df_a$last_pymnt_date <- dplyr::case_when(
grepl("Fully Paid", df_a$status) ~ df_a$maturity_dt,
TRUE ~ df_a$last_pymnt_date)
:
data.table