[R] Loop inside dplyr::mutate

Richard M. Heiberger rmh @end|ng |rom temp|e@edu
Sun May 10 03:40:28 CEST 2020


## I start with sim_data_wide

sim_data_wide <- tidyr::spread(sim_data, quarter, pd)

## and calculate wide
wide1 <- with(sim_data_wide, cbind(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)
                      )
)

## this simpler sequence gets the same value

A <- 1-sim_data_wide[,2:11]
B <- t(apply(A, 1, cumprod)[-1,])
wide2 <- cbind(sim_data_wide[,2], 1-B)
dimnames(wide2)[[2]] <- paste0("PC_", 1:10)

all.equal(wide1, wide2)


On Sat, May 9, 2020 at 9:28 PM Jeff Newmiller <jdnewmil using dcn.davis.ca.us>
wrote:

> 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.
>
> ______________________________________________
> 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.
>

	[[alternative HTML version deleted]]



More information about the R-help mailing list