Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R - how to replace parts of variable strings within data frame

I have a dataframe df:

var1 var2 "test" "testing" "esten" "etsen" "blest" "estten" 

Now I want to delete all "t" inside df to get:

var1 var2 "es" "esing" "esen" "esen" "bles" "esen" 

How do I do that?

like image 217
user670186 Avatar asked Mar 30 '11 13:03

user670186


People also ask

How do I remove part of a string in a column in R?

To remove a character in an R data frame column, we can use gsub function which will replace the character with blank. For example, if we have a data frame called df that contains a character column say x which has a character ID in each value then it can be removed by using the command gsub("ID","",as.

How do you replace a string element?

Python String | replace() replace() is an inbuilt function in the Python programming language that returns a copy of the string where all occurrences of a substring are replaced with another substring. Parameters : old – old substring you want to replace. new – new substring which would replace the old substring.


1 Answers

Use gsub

dat <- c("test", "testing", "esten", "etsen", "blest", "estten")  gsub("t", "", dat) [1] "es"    "esing" "esen"  "esen"  "bles"  "esen"  
like image 68
Chase Avatar answered Sep 20 '22 15:09

Chase