Replace multiple strings in one gsub() or chartr() statement in R?

前端 未结 6 1535
遇见更好的自我
遇见更好的自我 2020-11-30 07:28

I have a string variable containing alphabet[a-z], space[ ], and apostrophe[\'],eg. x <- \"a\'b c\" I want to replace apostrophe[\'] with blank[], and replac

6条回答
  •  感动是毒
    2020-11-30 08:29

    I would opt for a magrittr and/or dplyr solution, as well. However, I prefer not making a new copy of the object, especially if it is in a function and can be returned cheaply.

    i.e.

    return(
      catInTheHat %>% gsub('Thing1', 'Thing2', .) %>% gsub('Red Fish', 'Blue 
        Fish', .)
    )
    

    ...and so on.

提交回复
热议问题