Remove Newline from Character String in R (2 Examples)


On this page you’ll learn how to delete all line breaks (i.e. enter symbols) from character strings in the R programming language.

Table of contents:

Let’s get started!


Creation of Example Data

The first step is to create some data that we can use in the following examples:

my_string <- "aaa\nbbbbb\ncc\nd"                             # Create example character string
cat(my_string)                                               # Display example character string
# aaa
# bbbbb
# cc
# d

The previous output of the RStudio console shows that our example data is a single character string object containing multiple newline symbols (i.e. “\n”).


Example 1: Delete All Line Breaks from Character String Using gsub() Function

In Example 1, I’ll explain how to remove all enter breaks from our character string using the gsub function in R.

Have a look at the following R code:

my_string_new1 <- gsub("[\r\n]", "", my_string)              # Apply gsub() function
my_string_new1                                               # Print updated character string
# [1] "aaabbbbbccd"

As you can see based on the previous output of the RStudio console, we have created a new character string object called my_string_new1 that contains the characters of our input string without the newline symbol.

Note that we had to specify “[\r\n]” within the gsub function (instead of just “\n”) because the backslash is an escape character.


Example 2: Delete All Line Breaks from Character String Using str_replace_all() Function of stringr Package

Example 2 explains how to use the functions of the stringr package to remove line breaks from a character string.

We first need to install and load the stringr package, in order to use the functions that are included in the package.

install.packages("stringr")                                  # Install stringr package
library("stringr")                                           # Load stringr package

Now, we can use the str_replace_all function to delete all newlines:

my_string_new2 <- str_replace_all(my_string, "[\r\n]" , "")  # Apply str_replace_all() function
my_string_new2                                               # Print updated character string
# [1] "aaabbbbbccd"

The output is exactly the same as in the previous example. Whether you prefer to use the gsub or str_replace_all functions is a matter of taste.


Video & Further Resources

Have a look at the following video that I have published on my YouTube channel. I illustrate the content of this tutorial in the video:



Also, you may read the other articles of my homepage:


To summarize: This page has shown how to delete newlines from a string in R programming. In case you have further questions, don’t hesitate to let me know in the comments section. Furthermore, please subscribe to my email newsletter for regular updates on new tutorials.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.