Home > Net >  Remove linebreaks from a dataframe
Remove linebreaks from a dataframe

Time:10-15

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

CodePudding user response:

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"

CodePudding user response:

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| ", "")
  • Related