skip to Main Content

Using gsub(), I want to replace "nn" to "" in this text:

mystring<-"People Storm Seonul Airport as Citizens Leave\n\nCHOI JEONGHO\n\nSEOUL, South Korea, Jan. 31"

But, when I ran gsub("[r\n]", " ", mystring), it deletes all n in the text.

The output was:

"People Storm Seo ul  Airport as Citize s Leave    CHOI JEO GHO    SEOUL, South Korea, Ja . 31"

Could you teach me why it is so and how to delete only "nn"?

2

Answers


    1. You don’t want to use brackets because you’re trying to match a sequence of characters — brackets will match any of the characters individually, which is how you end up with no ns.
    2. To match a literal \n, you need to escape both backslashes, giving you \\n.
    gsub("(r)|(\\n)", " ", mystring)
    
    # "People Storm Seonul Airport as Citizens Leave  CHOI JEONGHO  SEOUL, South Korea, Jan. 31"
    
    Login or Signup to reply.
  1. Here’s an option with the stringr package. In R, you need to escape backslashes using \, so you’ll need to use \\n as your argument:

    stringr::str_remove_all(mystring, "\\n")
    
    "People Storm Seonul Airport as Citizens LeaveCHOI JEONGHOSEOUL, South Korea, Jan. 31"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search