i have this table:
aux_nov <- hist[, c(“contrato”, “importe”, “mob”, “fecha_pago”, “dias_atraso”)]
with this values in this columns, so…
contrato <- c(598047, 598047, 598047, 598047, 598047, 598047, 598047, 598047)
fecha_pago <- c(2023-10-21, NA, NA, NA, 2024-03-13, 2024-04-02, NA, NA)
seguimiento_mes <- c(2023-11-30, 2023-11-30, 2023-11-30, 2023-11-30, 2023-11-30, 2023-11-30, 2023-11-30, 2023-11-30)
I want this conditions:
If fecha_pago is NA then we check the previous date and if it is NA then it remains NA,
if the previous date is not NA then we see if that fecha_pago > seguimiento_mes then previous fecha_pago is placed, if fecha_pago < seguimiento_mes then NA
I have not been able to create these conditions for my payment_date column, I have tried with a buble but it gives me values that I am not looking for or it does not make any changes.
for example i try something like this:
last_valid_payment_date <- NULL
for (i in 1:nrow(aux_nov)) {
if (is.na(aux_nov$fecha_pago[i])) {
if (!is.na(last_valid_payment_date)) {
if (last_valid_payment_date > aux_nov$seguimiento_mes[i]) {
aux_nov$fecha_pago[i] <- last_valid_payment_date
} else {
aux_nov$fecha_pago[i] <- NA
}
}
} else {
if (aux_nov$fecha_pago[i] > aux_nov$seguimiento_mes[i]) {
last_valid_payment_date <- aux_nov$fecha_pago[i]
}
}
}
but 1. This code does not group me by id, so in the saved date it can save the other id’s so it can bring me wrong information
Brian Garcia Alvarez is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.