Replace specific characters within strings

Viewed 713548

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:

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

I start with just the first column; I want to produce the second column by removing the e's:

group       group.no.e
12357e      12357
12575e      12575
197e18      19718
e18947      18947
7 Answers
> library(stringi)                
> group <- c('12357e', '12575e', '12575e', ' 197e18',  'e18947')              
> pattern <- "e"  
> replacement <-  ""  
> group <- str_replace(group, pattern, replacement)      
> group 
[1] "12357"  "12575"  "12575"  " 19718" "18947" 

You can use chartr as well:

group$group.no.e <- chartr("e", "", group$group)
Related