我有一个向量,它包含数据帧的列名。我想清理那些绳子。
vec_of_names <- c("FIRST_column",
"another-column",
"ALLCAPS-column",
"cOLumn-with___specialsuffix",
"blah#4-column",
"ANOTHER_EXAMPLE___specialsuffix",
"THIS_IS-Misleading_specialsuffix")我特别想使用janitor::make_clean_names()来进行清理。
janitor::make_clean_names(vec_of_names)
[1] "first_column" "another_column"
[3] "allcaps_column" "c_o_lumn_with_specialsuffix"
[5] "blah_number_4_column" "another_example_specialsuffix"
[7] "this_is_misleading_specialsuffix"然而,,,我想应用以下规则:
___specialsuffix (即3个下划线和“专用后缀”)结尾时,- clean with `janitor::make_clean_names()` only the part BEFORE `___specialsuffix` (意思是从strsplit(x, "___specialsuffix")返回的值)。
- then keep the cleaned string pasted back to `___specialsuffix`.否则,如果字符串未以结束,则对整个字符串定期使用janitor::make_clean_names()清除.
因此,所需的输出为:
[1] "first_column" "another_column"
[3] "allcaps_column" "c_o_lumn_with___specialsuffix" ## elements [4] and [6]
[5] "blah_number_4_column" "another_example___specialsuffix" ## were handled according to rule #1
[7] "this_is_misleading_specialsuffix" ## outlined above非常感谢你的任何想法!
发布于 2021-01-21 13:58:33
vec_of_names <- c("FIRST_column",
"another-column",
"ALLCAPS-column",
"cOLumn-with___specialsuffix",
"blah#4-column",
"ANOTHER_EXAMPLE___specialsuffix",
"THIS_IS-Misleading_specialsuffix")
library(tidyverse)
suffix <- vec_of_names %>% str_extract(pattern = "___specialsuffix$") %>% replace_na("")
cleaned_without_suffix <- vec_of_names %>% str_remove("___specialsuffix$") %>% janitor::make_clean_names()
output <- paste0(cleaned_without_suffix, suffix)https://stackoverflow.com/questions/65828826
复制相似问题