Skip to content

Instantly share code, notes, and snippets.

@brshallo
Created February 20, 2019 02:43
Show Gist options
  • Save brshallo/8746f5b05549e3cda12ee733580a5bef to your computer and use it in GitHub Desktop.
Save brshallo/8746f5b05549e3cda12ee733580a5bef to your computer and use it in GitHub Desktop.
function to convert contrasts in factors to deviation coding
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