Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (7.3k points)

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

Here is 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

1 Answer

0 votes
by

You can use the gsub() function that replaces all matches of a string.

The basic syntax is given below:

gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,

  fixed = FALSE, useBytes = FALSE)

 

In your case:

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

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

 

Output:

   group group.no.e

1 12357e     12357

2 12575e     12575

3 197e18     19718

4 e18947     18947

Browse Categories

...