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"
}
}
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