val val - 16 days ago 8
R Question

Why is this gsub not working in R

I want to replace every word beginning with Jo with the word Joburg, however this code isn't working and I don't understand why.

gsub("^Jo\\w+","Joburg",DF[,4:9])


My data frame has 10 columns and i'm looking to do the replacement in columns 4 to 9, all of which are factors.

When I run unique() afterwards i still get the original list of distinct words starting with Jo, instead of only the word Joburg throughout.

Answer

If we are working on a data.frame, then loop through the columns and apply the gsub

DF[4:9] <- lapply(DF[4:9], function(x) gsub("^Jo\\w+","Joburg",x))

This can also be done with sub and without an anonymous function call

DF[4:9] <- lapply(DF[4:9], sub, pattern = "^Jo\\w+", replacement = "Joburg")

data

set.seed(24)
DF <- as.data.frame(matrix(sample(c("Joan", "abf", "Jedi", "acf"), 9*4,
                       replace=TRUE), ncol=9))
Comments