str_replace_all 不是一次全部迭代地替换命名向量元素 [英] str_replace_all replacing named vector elements iteratively not all at once

查看:28
本文介绍了str_replace_all 不是一次全部迭代地替换命名向量元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个长字符串:pneumonoultramicroscopicsilicovolcanoconiosis.我想使用 stringr::str_replace_all 将某些字母替换为其他字母.根据文档, str_replace_all 可以采用命名向量并将名称替换为值.这适用于 1 次替换,但对于多次替换,它似乎是迭代进行的,因此结果是对上一次迭代的替换.我不确定这是预期的行为.

Let's say I have a long character string: pneumonoultramicroscopicsilicovolcanoconiosis. I'd like to use stringr::str_replace_all to replace certain letters with others. According to the documentation, str_replace_all can take a named vector and replaces the name with the value. That works fine for 1 replacement, but for multiple it seems to do it iteratively, so the result is a replacement of the prelast iteration. I'm not sure this is the intended behaviour.

library(tidyverse)
text_string = "developer"
text_string %>% 
  str_replace_all(c(e ="X")) #this works fine
[1] "dXvXlopXr"
text_string %>% 
  str_replace_all(c(e ="p", p = "e")) #not intended behaviour
[1] "develoeer"

想要的结果:

[1] "dpvploepr"

我通过引入一个新角色得到的:

Which I get by introducing a new character:

text_string %>% 
  str_replace_all(c(e ="X", p = "e", X = "p"))

这是一种可用的解决方法,但很难推广.这是一个错误还是我的期望错误?

It's a usable workaround but hardly generalisable. Is this a bug or are my expectations wrong?

我还希望能够同时用 n 个其他字母替换 n 个字母,最好使用两个向量(如旧"和新")或命名向量作为输入.

I'd like to also be able to replace n letters with n other letters simultaneously, preferably using either two vectors (like "old" and "new") or a named vector as input.

reprex 已编辑以便于人类阅读

推荐答案

我正在开发一个包来处理该类型的问题.这比 qdap::mgsub 函数更安全,因为它不依赖于占位符.它完全支持正则表达式作为匹配和替换.您提供一个命名列表,其中名称是要匹配的字符串,而它们的值是替换字符串.

I'm working on a package to deal with the type of problem. This is safer than the qdap::mgsub function because it does not rely on placeholders. It fully supports regex as the matching and the replacement. You provide a named list where the names are the strings to match on and their value is the replacement.

devtools::install_github("bmewing/mgsub")
library(mgsub)
mgsub("developer",list("e" ="p", "p" = "e"))
#> [1] "dpvploepr"

qdap::mgsub(c("e","p"),c("p","e"),"developer")
#> [1] "dpvploppr"

这篇关于str_replace_all 不是一次全部迭代地替换命名向量元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆