[R] Loop inside dplyr::mutate

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Sun May 10 03:17:24 CEST 2020


Does this help?

sim_wide2 <- (
    sim_data
%>% arrange( borrower_id, quarter )
%>% group_by( borrower_id )
%>% mutate( cumpd = 1 - cumprod( 1 - pd ) )
%>% ungroup()
%>% mutate( qlbl = paste0( "PC_", quarter ) )
%>% select( borrower_id, qlbl, cumpd )
%>% spread( qlbl, cumpd )
)

On May 9, 2020 4:45:40 PM PDT, 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.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list