% replace_na("-6ren">
gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 08:13:30 24 4
gpt4 key购买 nike

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

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 个下划线和“特殊后缀”),
  • janitor::make_clean_names() 清洁只有之前的部分 ___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)

    关于r - 使用 janitor::make_clean_names() 自定义清理字符串向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65828826/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com