How to change part of string in R

184 views Asked by At

I have a question regarding string manipulation in R. I have a data frame with two columns:

NAME          DATE
xxx-test-xx   2015-02-03
Frank         2015-02-01
Steve         2014-09-31
132-test-ggg  2012-12-09

I want to change all cases in column NAME which contains word "test" into one name - for example "TEST". I have prepared code as below but it doesn't work - appropriate cases are not found as they should be. Observations in NAME variable do not have any particular pattern. Could you tell me how to fix it?

dataset$EMAIL <- as.character(dataset$EMAIL) 

for (i in 1:length(dataset)) {
  if(grepl("test", dataset$EMAIL[i], ignore.case=TRUE))  {
    dataset$EMAIL[i] <- "TEST"
  }
}
2

There are 2 answers

0
thothal On

First things first, you do not need to loop over all entries in the column you can rely on R being vectorized.

Then you could simply use gsub

gsub(".*test.*", "TEST", dataset$EMAIL)
0
C_Z_ On

When you do length(dataset) you will return the number of columns in your dataframe, not the number of rows. To fix your loop, you can do 1:nrow(dataset). But actually you can get rid of the for loop entirely in this case and do

dataset$EMAIL <- as.character(dataset$EMAIL) 
dataset$EMAIL[grepl("test", dataset$EMAIL, ignore.case=T)] <- "TEST"