Replace specific characters within strings

前端 未结 6 1567
悲哀的现实
悲哀的现实 2020-11-22 08:05

I would like to remove specific characters from strings within a vector, similar to the Find and Replace feature in Excel.

Here are the data I start with:<

6条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 08:45

    Summarizing 2 ways to replace strings:

    group<-data.frame(group=c("12357e", "12575e", "197e18", "e18947"))
    

    1) Use gsub

    group$group.no.e <- gsub("e", "", group$group)
    

    2) Use the stringr package

    group$group.no.e <- str_replace_all(group$group, "e", "")
    

    Both will produce the desire output:

       group group.no.e
    1 12357e      12357
    2 12575e      12575
    3 197e18      19718
    4 e18947      18947
    

提交回复
热议问题