I'm trying to clean a dataset's names. I've used janitor::clean_names()
to start. However, I still have abbreviations that I would like to separate out with an underscore _
. I have code that works using rename_with(~str_replace(.x, "gh", "gh_"), .cols = starts_with("gh"))
, however there are many abbreviations and it would be good to find a way to map
or otherwise functionalize this process.
dat <- tibble(ghrisk_value = c(1,2),
ghrisk_corrected = c(2,3),
devpolicy_value = c(4,5),
devpolicy_corrected = c(5,6))
# code works but not functionalized
dat %>%
rename_with(~str_replace(.x, "gh", "gh_"), .cols = starts_with("gh")) %>%
rename_with(~str_replace(.x, "dev", "dev_"), .cols = starts_with("dev")) %>%
names()
# attempt at map...
abbr_words <- c("gh", "dev")
map(dat, ~rename_with(str_replace(.x, abbr_words, str_c(abbr_words, "_")))