Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

R: Removing a slash and one followed character from strings in R

I have some race data where the values contain ‘\n’, such as in ‘Biracial or Multiracial\nBlack or African American\nWhite or Caucasian’ or ‘White or Caucasian\nBlack or African American’. Is there a way to remove the slash and the followed letter n? (preferably using str_remove or gsub)

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use double \ for escaping.

For instance:

library(stringr)

str_remove("Multiracial\nBlack" , "\\n")

And the same for gsub():

gsub("\\n", "", "Multiracial\nBlack")

Output:

[1] "MultiracialBlack"

I hope you consider \n to be line brake, not two symbols.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading