14
$\begingroup$

I have a dataset like the one below. I want to remove all characters after the character ©. How can I do that in R?

data_clean_phrase <- c("Copyright © The Society of Geomagnetism and Earth", 
"© 2013 Chinese National Committee ")

data_clean_df <- as.data.frame(data_clean_phrase)
$\endgroup$
  • $\begingroup$ Is it after a specific character, or after a specific index? $\endgroup$ – Dawny33 Nov 19 '15 at 13:39
  • $\begingroup$ After a specific character: © $\endgroup$ – Hamideh Nov 19 '15 at 16:24
  • $\begingroup$ Then, seems like the existing answer solved your question :) $\endgroup$ – Dawny33 Nov 19 '15 at 16:26
16
$\begingroup$

For instance:

 rs<-c("copyright @ The Society of mo","I want you to meet me @ the coffeshop")
 s<-gsub("@.*","",rs)
 s
 [1] "copyright "             "I want you to meet me "

Or, if you want to keep the @ character:

 s<-gsub("(@).*","\\1",rs)
 s
 [1] "copyright @"             "I want you to meet me @"

EDIT: If what you want is to remove everything from the last @ on you just have to follow this previous example with the appropriate regex. Example:

rs<-c("copyright @ The Society of mo located @ my house","I want you to meet me @ the coffeshop")
s<-gsub("(.*)@.*","\\1",rs)
s
[1] "copyright @ The Society of mo located " "I want you to meet me "

Given the matching we are looking for, both sub and gsub will give you the same answer.

$\endgroup$
  • $\begingroup$ Thank you. and What if I want to do it for the last © in the text. Consider this: c(" © aaa © bbb") --> c( "© aaa") $\endgroup$ – Hamideh Nov 19 '15 at 15:36
  • $\begingroup$ @HamidehIraj You can make use of regexes for executing that. $\endgroup$ – Dawny33 Nov 19 '15 at 16:26
  • 1
    $\begingroup$ You are welcome. Once you get use to regex you'll see that it is as easy to remove from the last @ char. I've edited my answer to include this case as well. $\endgroup$ – MASL Nov 19 '15 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.