Replace specific characters within strings

前端 未结 6 1522
悲哀的现实
悲哀的现实 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:31

    Regular expressions are your friends:

    R> ## also adds missing ')' and sets column name
    R> group<-data.frame(group=c("12357e", "12575e", "197e18", "e18947"))  )
    R> group
       group
    1 12357e
    2 12575e
    3 197e18
    4 e18947
    

    Now use gsub() with the simplest possible replacement pattern: empty string:

    R> group$groupNoE <- gsub("e", "", group$group)
    R> group
       group groupNoE
    1 12357e    12357
    2 12575e    12575
    3 197e18    19718
    4 e18947    18947
    R> 
    

提交回复
热议问题