Ed_Gravy
Ed_Gravy

Reputation: 2033

Remove linebreaks from a dataframe

Based on the data and code below how can I remove all the line breaks from the data?

Data:

  ID            a        
  0BX-164463    abc \n def
  0BX-164463    hijk \n lmnop
  0BX-164464    qrst \n uvwxyz
  0BX-164464    12345 \n abcdefg \n gravy

Upvotes: 1

Views: 133

Answers (2)

KacZdr
KacZdr

Reputation: 1599

Other option with str_replace_all:

library(stringr)

str_replace_all(data$a, "\n", "")

or also with the removal of spaces:

str_replace_all(data$a, "\n| ", "")

Upvotes: 1

Maël
Maël

Reputation: 52004

gsub("\\n", "", "abc \n def")
#[1] "abc  def"

If you want to remove the spaces as well:

gsub("\\n|\\s", "", "abc \n def")
#[1] "abcdef"

Upvotes: 1

Related Questions