Created
February 20, 2019 02:43
-
-
Save brshallo/8746f5b05549e3cda12ee733580a5bef to your computer and use it in GitHub Desktop.
function to convert contrasts in factors to deviation coding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(tidyverse) | |
contrast_deviation <- function(vec_fct){ | |
new_contrast <- unique(vec_fct) %>% length() %>% contr.sum() | |
contrasts(vec_fct) <- new_contrast | |
vec_fct | |
} | |
# convert all char columns to factors that use deviation coding | |
mpg %>% | |
mutate_if(is.character, as.factor) %>% | |
mutate_if(is.factor, contrast_deviation) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment