0

Dataframe:

a<- as.character(rep("Sick (Atlas) (7:24)", 6))
b<- as.character(rep("20/08/2014", 6))
id<- as.character(rep("234", 6))
df<- data.frame(a,b,id)
df$a<-as.character(df$a)

enter image description here

I would like to remove (Atlas). I tried

df <- as.data.frame(sapply(df,gsub,pattern="(Atlas)",replacement=""))

but it removes only Atlas and leaves brackets:

enter image description here

After reading the help for gsub, I assume that brackets are not regular expressions. So I tried to use backslash, but it did not work either.

df <- as.data.frame(sapply(df,gsub,pattern="\(\Atlas\)\",replacement=""))

Any help would be much appreciated.

Jaap
  • 81,064
  • 34
  • 182
  • 193
Vasile
  • 1,017
  • 2
  • 10
  • 19
  • 1
    you don't need `sapply` for that. you can either escape the brackets or use `fixed=TRUE` – Cath Jan 26 '16 at 13:05
  • What is your expected output? If you need only `sick`, then `sub('\\s+.*', '', df$a)` – akrun Jan 26 '16 at 13:05
  • 2
    try `df$a <- gsub(pattern="\\(Atlas\\)",replacement="",df$a)` – scoa Jan 26 '16 at 13:05
  • Thanks for the answers. They all work fine. And I am sorry I posted a duplicate and stirred such a discussion. I had looked on the forum, but did not think that the questions I saw were answering my question. But I admit the omission. Could you please provide the link to the question that I duplicated? – Vasile Jan 26 '16 at 14:02
  • @Vasile the link should be on top of your question, you may need to refresh the page. anyway : http://stackoverflow.com/questions/27721008/how-do-i-deal-with-special-characters-like-in-my-regex – Cath Jan 26 '16 at 14:06
  • 2
    @Cath Thank you. I saw that answer, but I quickly thought that the solution would remove all my brackets and whatever between them. Since I had some other parenthesis that I needed to keep, I just jumped to something else. But perhaps I could have explored it more and use for my question. Will try to do this in the future, before posting – Vasile Jan 26 '16 at 14:22

0 Answers0