我有一个包含数据框列名称的向量。我想清理那些弦。
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个下划线和“ specialsuffix”)结尾时,
janitor::make_clean_names()
仅使用部分BEFORE进行清理___specialsuffix
(即,从返回的值strsplit(x, "___specialsuffix")
)。
然后将清理后的字符串粘贴回___specialsuffix
。
否则,如果字符串不以结尾___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
非常感谢您的任何想法!
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句