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

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


Use gsub

dat <- c("test", "testing", "esten", "etsen", "blest", "estten")gsub("t", "", dat)[1] "es"    "esing" "esen"  "esen"  "bles"  "esen" 


You can do this with gsub and using sapply to apply it per variable:

df <- data.frame(     var1 = c("test","esten","blest"),    var2 = c("testing","etsen","esttem"))df2 <- as.data.frame(sapply(df,gsub,pattern="t",replacement=""))df2  var1  var21   es esing2 esen  esen3 bles  esem