Last active
February 18, 2021 17:12
-
-
Save shannonpileggi/66fa2ab4dda7318cb78bf7d077f4c8f7 to your computer and use it in GitHub Desktop.
A demonstration of mutate_at
This file contains hidden or 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) | |
# values to mimic 1 to 7 rating scale with 99 as missing | |
values <- c(1:7, 99) | |
# for reproducibility | |
set.seed(0215) | |
# example data frame with two variables | |
dat <- tibble("A_1" = sample(values, 10, replace = T), | |
"A_2" = sample(values, 10, replace = T)) | |
# view example data | |
dat | |
# in original variable names, re-code 99 to missing | |
# for this example you could use mutate_all, but I generally | |
# use mutate_at to specify variables | |
dat %>% | |
mutate_at(vars(matches("A")), na_if, 99) | |
# create new variables with re-coded values | |
# the name of the function in list assigns a name to the new variable | |
dat %>% | |
mutate_at(vars(matches("A")), list(clean = ~ na_if(., 99))) | |
# you can use this with multiple steps to track a data cleaning process | |
# a top 2 box is a score of 6 or 7 on a 1-7 rating scale | |
dat %>% | |
mutate_at(vars(matches("A")), list(clean = ~ na_if(., 99))) %>% | |
mutate_at(vars(matches("clean")), list( | |
box = ~ case_when(is.na(.) ~ NA_character_, | |
. %in% 6:7 ~ "top 2", | |
. %in% 1:5 ~ "other") | |
)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment