使用janitor :: make_clean_names()自定义清洗字符串向量

艾曼

我有一个包含数据框列名称的向量。我想清理那些弦。

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"

但是,我要应用以下规则:

  1. 当字符串以___specialsuffix(即3个下划线和“ specialsuffix”)结尾时

    • janitor::make_clean_names()仅使用部分BEFORE进行清理___specialsuffix
      (即,从返回的值strsplit(x, "___specialsuffix"))。

    • 然后将清理后的字符串粘贴回___specialsuffix

  2. 否则,如果字符串不以结尾___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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在R janitor包中使用`make_clean_names`保留重复项

如何在 R 的“janitor”包中将 `clean_names` 与 `row_to_names` 结合使用

使用字符串向量在 R 中循环自定义 ifelse 函数

使用自定义字符串定义符号变量

使用自定义比较功能使用std :: sort()对向量字符串进行排序时遇到问题

对字符串使用自定义比较器

使用自定义输入乘以字符串的输出

使用自定义uisearchbar搜索字符串

使用sed自定义字符串空间

如何使用自定义字符集递增字符串

无法使用 f-Strings 将字符串转换为自定义字符串

使用自定义提示字符串使用zsh启动新的终端实例

如何使用自定义创建的字符串原型方法?

使用自定义排序顺序在Unix中对字母数字字符串进行排序

添加自定义激活功能以与字符串一起使用

为什么要使用自定义的比较来TreeSet的是打破字符串的平等对象?

在C ++中使用自定义字符串类作为映射键值

使用自定义顺序对字符串数组进行排序

使用Python中的自定义字符串从数据框日期列返回会计季度

Python:使用自定义分隔符格式化字符串

自定义日期字符串使用杰克逊JsonFormat模式

无法使用多个字符串数组构建自定义列表视图

使用自定义分隔符将数字格式化为字符串?

在python3中使用自定义字符串执行

使用自定义类型将JSON字符串转换为Map

使用自定义名称空间在Java中创建XML字符串

如何使用自定义的“字符串”重命名 x 标签?

使用自定义字符串初始化静态最终日期

使用Parsley.js自定义验证器检查字符串