Reputation: 49
I have a column in a data frame clean.data$Country
. One of the cells in that is currently written as USA(For Country)
. This is row 3216, so the observation can be viewed with clean.data$Country[3216]
.
I am trying to insert a space between "USA" and the parenthesis, but neither of the following methods have worked:
clean.data$Country <- str_replace(clean.data$Country, "USA(For Territories)", "USA (For Territories)")
clean.data$Country <- gsub("USA(For Territories)", "USA (Territories)", clean.data$Country)
What alternative methods are there for me to do this? I have tried closing my .rmd file, clearing my workspace of all objects, and restarting RStudio.
Upvotes: 0
Views: 364
Reputation: 193517
Add fixed = TRUE
or escape your parentheses:
x <- "USA(For Territories)"
## Your attempt
gsub("USA(For Territories)", "USA (Territories)", x)
# [1] "USA(For Territories)"
## fixed = TRUE
gsub("USA(For Territories)", "USA (Territories)", x, fixed = TRUE)
# [1] "USA (Territories)"
## Escaping the parentheses
gsub("USA\\(For Territories\\)", "USA (Territories)", x)
# [1] "USA (Territories)"
Using str_replace
, you can try:
library(stringr)
str_replace(x, "USA\\(For Territories\\)", "USA (Territories)")
# [1] "USA (Territories)"
str_replace(x, fixed("USA(For Territories)"), "USA (Territories)")
# [1] "USA (Territories)"
Upvotes: 0
Reputation: 521178
If you are certain of this single data point error only, just make an assignment:
clean.data$Country[3216] <- "USA (For Territories)"
If you want a more general solution, which could fix this problem in multiple places, then use a regex pattern which does that:
clean.data$Country <- sub("^(\\w+)\\((.*)\\)$", "\\1 (\\2)", clean.data$Country)
Upvotes: 1