Reputation: 133
I have the following data.
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)
I want to make the last_pymnt_date
same as maturity_dt
if status
variable includes the expression "Fully Paid". If I run the following line it makes last_pymnt_date
variable some numbers.
df_a$last_pymnt_date <- ifelse(grepl("Fully Paid", df_a$status),
df_a$maturity_dt,
df_a$last_pymnt_date)
Any suggestion?
Thanks
Upvotes: 0
Views: 44
Reputation: 14774
Solution using 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
's case_when
is also a good alternative to ifelse
, especially if you have more conditions:
df_a$last_pymnt_date <- dplyr::case_when(
grepl("Fully Paid", df_a$status) ~ df_a$maturity_dt,
TRUE ~ df_a$last_pymnt_date)
Or using data.table
:
library(data.table)
df_a <- setDT(df_a)[grepl("Fully Paid", status), last_pymnt_date := maturity_dt]
Upvotes: 2
Reputation: 11150
Here's a simple way to do this. Just separated out grepl
test to improve readability.
test <- grepl("Fully Paid", df_a$status)
df_a$last_pymnt_date[test] <- df_a$maturity_dt[test]
Upvotes: 3