How to search for multiple strings and replace them with nothing within a list of strings

前端 未结 3 864
借酒劲吻你
借酒劲吻你 2021-02-05 17:52

I have a column in a dataframe like this:

npt2$name
#  [1] \"Andreas Groll, M.D.\"
#  [2] \"\"
#  [3] \"Pan-Chyr Yang, PHD\"
#  [4] \"Suh-Fang Jeng, Sc.D\"
#  [5         


        
3条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 18:21

    With a single ugly regex:

     gsub('[M,P].?D.?','',npt2$name)
    

    Which says, find characters M or P followed by zero or one character of any kind, followed by a D and zero or one additional character. More explicitly, you could do this in three steps:

    npt2$name <- gsub('MD','',npt2$name)
    npt2$name <- gsub('M\\.D\\.','',npt2$name)
    npt2$name <- gsub('PhD','',npt2name)
    

    In those three, what's happening should be more straight forward. the second replacement you need to "escape" the period since its a special character.

提交回复
热议问题