TransWikia.com

WOE in a magrittr Pipe

Stack Overflow Asked by gojomoso on December 6, 2020

How can I get the weight of evidence code below into a magrittr pipeline (df %>%)? Nothing I’ve tried seems to work.

df:

library(Information)
library(magrittr)


#df
a = c("aa", "bb", "cc", "aa", "aa", "aa", "bb", "cc", "bb", "bb") 
b = c("aa", "bb", "cc", "aa", "aa", "aa", "bb", "cc", "bb", "bb") 
c = c("aa", "bb", "cc", "aa", "aa", "aa", "bb", "cc", "bb", "bb") 
d = c("aa", "bb", "cc", "aa", "aa", "aa", "bb", "cc", "bb", "bb") 
e = c(1, 0, 1, 0, 0, 0, 1, 1, 1, 1)
df = data.frame(a,b,c,d,e)

cols <- c("c","d")

code for pipe

Enc <- create_infotables(data=subset(df, select = -c(a)), y="e", parallel=FALSE)

for(i in names(df[,cols])) {
  df[,i] <- Enc$Tables[[i]][match(df[,i], Enc$Tables[[i]][,1]), 4]
}

2 Answers

We could use map2. Here, we are checking for corresponding elements of the two objects i.e. column with a column from the list of data.frame (Enc$Tables). So, an option is map2

library(purrr)
df[cols] <- map2_dfc(df[cols], Enc$Tables[cols], ~  .y[match(.x, .y[,1]), 4])

To use it in a pipe

library(magrittr)
library(dplyr)
df %>%
    select(-a) %>%
    create_infotables(data = ., y = "e", parallel = FALSE) %>%       
    pluck("Tables") %>%
    magrittr::extract(cols) %>%
    map2_dfc(df[cols], ., ~ .y[match(.x, .y[,1]), 4]) 

Correct answer by akrun on December 6, 2020

If you actually want to use the magritte pipe you can also try this: define a function which takes the column name as input and use mutate() and across() to apply it to all column names in vector cols.

library(dplyr)

enc <- function(col_name) {
  Enc$Tables[[col_name]][match(df[,col_name], Enc$Tables[[col_name]][,1]), 4]
}

df %>% 
  mutate(across(cols, function(x) enc(cur_column())))

Answered by pieterbons on December 6, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP