[R] Loop inside dplyr::mutate

Axel Urbiz @xe|@urb|z @end|ng |rom gm@||@com
Sun May 10 01:45:40 CEST 2020


Hello, 

Is there a less verbose approach to obtaining the PC_i variables inside the mutate?

library(tidyverse)
sim_data <- data.frame(borrower_id = sort(rep(1:10, 20)),
                       quarter = rep(1:20, 10),
                       pd = runif(length(rep(1:20, 10)))) # conditional probs
 
sim_data_wide <- tidyr::spread(sim_data, quarter, pd)  
colnames(sim_data_wide)[-1] <- paste0("P_", colnames(sim_data_wide)[-1])
      
# Compute cumulative probs
sim_data_wide <- sim_data_wide %>%
                  mutate(PC_1 = P_1,
                         PC_2 = 1-(1-P_1)*(1-P_2),
                         PC_3 = 1-(1-P_1)*(1-P_2)*(1-P_3),
                         PC_4 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4),
                         PC_5 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5),
                         PC_6 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6),
                         PC_7 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7),
                         PC_8 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8),
                         PC_9 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9),
                         PC_10 = 1-(1-P_1)*(1-P_2)*(1-P_3)*(1-P_4)*(1-P_5)*(1-P_6)*(1-P_7)*(1-P_8)*(1-P_9)*(1-P_10)
                        )


Thanks,
Axel.
	[[alternative HTML version deleted]]



More information about the R-help mailing list