使用看门人 clean_names 保留特定字符

发布于 2025-01-09 08:18:40 字数 388 浏览 1 评论 0原文

我正在使用 janitor clean_names()/make_clean_names() 并希望保留某些字符。我认为 replace 是我应该使用的参数,并且适用于某些情况。例如,我可以用零替换破折号:

> janitor::make_clean_names("x-x", replace = c(`-` = "0"))
[1] "x0x"

但是,似乎没有办法保留破折号(或其他特殊字符):

> janitor::make_clean_names("x-x", replace = c(`-` = "-"))
[1] "x_x"

I am using janitor clean_names()/make_clean_names() and would like to preserve certain characters. I think replace is the argument I should be using and that works for some cases. For example, I can replace dashes with zeros:

> janitor::make_clean_names("x-x", replace = c(`-` = "0"))
[1] "x0x"

However, there does not seem to be a way to keep dashes (or other special characters):

> janitor::make_clean_names("x-x", replace = c(`-` = "-"))
[1] "x_x"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洒一地阳光 2025-01-16 08:18:40

使用 make_clean_names 的主要原因之一是返回一个无需使用反引号即可解析的标准名称。当存在非标准字符时,该包使用 _ 作为默认替换。因此,即使我们在 replace 中提供了一个命名的 vector,它也会在代码中更改为 _,正如文档中所说的顺序操作为

操作顺序为:进行替换、(可选)ASCII 转换、删除初始空格和标点符号、应用 base::make.names()、应用 Snakecase::to_any_case 以及添加数字后缀以解析任何重复的名称。

janitor::make_clean_names("x-x", replace = c(`-` = "~"), use_make_names = FALSE)
[1] "x_x"
janitor::make_clean_names("x-x", replace = c(`-` = "-"), use_make_names = FALSE)
[1] "x_x"

一个选项是指定 sep_out 返回感兴趣的字符

janitor::make_clean_names("x-x",  sep_out = "-")
[1] "x-x"
janitor::make_clean_names("x-x",  sep_out = "~")
[1] "x~x"

使用 make_clean_names 保留非标准字符的选项是用唯一的标准字符替换,然后替换稍后的独特字符/单词

gsub("_change_", "-", janitor::make_clean_names("x-x:x~x", 
    replace = c(`-` = "_change_")))
[1] "x-x_x_x"

One of the main reasons to use make_clean_names is to return a standard name that can be parsed without having to use backquotes. The package uses _ as default replacement when there is a non-standard character. Therefore, even if we provide a named vector in the replace, it will be changed in the code to _ as the documentation says the order of operations as

The order of operations is: make replacements, (optional) ASCII conversion, remove initial spaces and punctuation, apply base::make.names(), apply snakecase::to_any_case, and add numeric suffixes to resolve any duplicated names.

janitor::make_clean_names("x-x", replace = c(`-` = "~"), use_make_names = FALSE)
[1] "x_x"
janitor::make_clean_names("x-x", replace = c(`-` = "-"), use_make_names = FALSE)
[1] "x_x"

An option is to specify the sep_out to return a character of interest

janitor::make_clean_names("x-x",  sep_out = "-")
[1] "x-x"
janitor::make_clean_names("x-x",  sep_out = "~")
[1] "x~x"

An option to preserve the non-standard character using make_clean_names would be to replace with a unique standard character and then replace the unique character/words later

gsub("_change_", "-", janitor::make_clean_names("x-x:x~x", 
    replace = c(`-` = "_change_")))
[1] "x-x_x_x"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文