有条件地更新数据框中的日期

时间:2018-11-01 23:23:09

标签: r if-statement

我有以下数据。

 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_dtstatus相同。如果我运行以下行,它将使last_pymnt_date变成一些数字。

df_a$last_pymnt_date <- ifelse(grepl("Fully Paid", df_a$status),
                               df_a$maturity_dt,
                               df_a$last_pymnt_date)

有什么建议吗?

谢谢

2 个答案:

答案 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