str_replace_all replacing named vector elements iteratively not all at once

后端 未结 4 2039
误落风尘
误落风尘 2021-01-18 09:15

Let\'s say I have a long character string: pneumonoultramicroscopicsilicovolcanoconiosis. I\'d like to use stringr::str_replace_all to replace certain letters w

4条回答
  •  没有蜡笔的小新
    2021-01-18 09:30

    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"
    

提交回复
热议问题