[R] Loop inside dplyr::mutate

Fox, John j|ox @end|ng |rom mcm@@ter@c@
Sun May 10 02:46:08 CEST 2020


Dear Axel,

Assuming that you're not wedded to using mutate():

> D1 <- 1 - as.matrix(sim_data_wide[, 2:11])
> D2 <- matrix(0, 10, 10)
> colnames(D2) <- paste0("PC_", 1:10)
> for (i in 1:10) D2[, i] <- 1 - apply(D1[, 1:i, drop=FALSE], 1, prod)
> all.equal(D2, as.matrix(sim_data_wide[, 22:31]))
[1] TRUE 

I hope this helps,
 John

> On May 9, 2020, at 7:45 PM, Axel Urbiz <axel.urbiz using gmail.com> wrote:
> 
> 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]]
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list