Here are the code I wanted to execute:
feature_risk_col <-
loan_data %>%
select(!where(is.numeric)) %>% names()
for(i in feature_risk_col){
f_risk <-
loan_data %>%
group_by(i) %>%
summarize(rate = mean(risky_loan), count = n()) %>%
ungroup() %>%
mutate(diff = rate - g_risk,
risk = rate / g_risk)
paste("Feature Risk of", i)
print(f_risk)
print(" ")
}
The column names from feature_risk_col doesn't copied into the group_by() function when the for loop runs. Instead it asks for column named i (Column i
is not found).
Is this kind of problem better done using apply family function rather than for loop? Why? If so, I'm not sure how, should I create a function to be used in the apply family function?