0

I have a dataframe with a column that includes these character strings:

Rear-Naked Choke 
Submission (Rear-Naked Choke 
Rear Naked Choke

Example:

                  Method
1             Rear-Naked Choke
2             Rear Naked Choke
3             Rear Naked Choke
4 Submission (Rear-Naked Choke
5             Rear Naked Choke
6             Rear Naked Choke

I want them all to be "Rear-Naked Choke" so that I can count all under one category. In other words, I just want the entire column to read "Rear-Naked Choke".

Desired output:

                  Method
1             Rear-Naked Choke
2             Rear-Naked Choke
3             Rear-Naked Choke
4             Rear-Naked Choke
5             Rear-Naked Choke
6             Rear-Naked Choke

What I tried:

str_replace_all(c("Rear-Naked Choke" = "Rear-Naked Choke", "Submission (Rear-Naked Choke)" = "Rear-Naked Choke", "Rear Naked Choke)" = "Rear-Naked Choke"))

And:

gsub("Submission (Rear-Naked Choke)", "Rear-Naked Choke", data$column)

gsub doesn't work when I want to replace "(".

Do you know how to fix this?

MetaPhilosopher
  • 131
  • 2
  • 9
  • 2
    When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. Include a `dput()` of your data so we know exactly what's in the column. Show the code you tried. – MrFlick Feb 16 '18 at 15:56
  • 1
    Do you just want the entire column to read "Rear-Naked Choke"? – leeum Feb 16 '18 at 15:57
  • I'd do something like `x[grepl("Rear-Naked Choke", x)] <- "Rear-Naked Choke"`. That will replace any values that contain the phrase with the phrase itself. – Gregor Thomas Feb 16 '18 at 15:59
  • Your replacement of `(` isn't working because it's a regex special character. You can either escape it with two backslashes in the pattern or set `fixed = T` if you are looking for an exact match and not using fancy regex. – Gregor Thomas Feb 16 '18 at 16:01
  • Gregor, that solution worked, thank you! – MetaPhilosopher Feb 16 '18 at 17:34

1 Answers1

0

In order to replace a special character in R you have to make it this way:

gsub("\\(","","(hello") #This will give you "hello"

My recomendation for your problem is that first get rid of every special chacrater with gsub like this:

df$mycol <- gsub("[[:punct:]]","",df$mycol)

then after that it will be easier for you to replace any string in the column.

type ?regex in your R console for more information.

Víctor Cortés
  • 473
  • 4
  • 9