Hack-R
Hack-R

Reputation: 23241

Unexpected results from R's gsub function

gsub doesn't seem to be working as expected for me.

My data is a character vector which has a lot of values with countries' names in this format: "count(Country_Desc).Afghanistan". The full data can be found here:

https://gist.github.com/anonymous/1fa2d94b21a684139ffa

I was going to use gsub to leave only the actual countries' names for the elements in that format:

gsub("count(Country_Desc).", "", nms)

To my surprise this doesn't do anything; the result is the same as the original. I tried a few variations of this but to no avail.

I notice that if, just as an experiment, I run this:

gsub("Western Sahara", "", nms)

then it removes that country's name as expected (unfortunately that's not what I need to do).

Upvotes: 1

Views: 94

Answers (1)

Joshua Ulrich
Joshua Ulrich

Reputation: 176738

Escape the parentheses, or set fixed=TRUE. Parenthesis are used for grouping regular expressions.

R> gsub("count\\(Country_Desc\\).", "", nms)
R> gsub("count(Country_Desc).", "", nms, fixed=TRUE)

Upvotes: 3

Related Questions