val val - 1 year ago 163
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.


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 Source

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


DF <-"Joan", "abf", "Jedi", "acf"), 9*4,
                       replace=TRUE), ncol=9))
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download