【发布时间】:2020-12-23 01:15:28
【问题描述】:
我正在尝试清理数据集的名称。我用janitor::clean_names() 开始。但是,我仍然有一些缩写,我想用下划线 _ 分开。我有使用rename_with(~str_replace(.x, "gh", "gh_"), .cols = starts_with("gh")) 的代码,但是有很多缩写,最好找到map 的方法或以其他方式功能化此过程。
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, "_")))
【问题讨论】: