Add New Line to Text File in R (Example)

 

In this post, I’ll show how to append a new line to an already existing TXT file in the R programming language.

Table of contents:

Let’s get started…

 

Constructing Example Data

The TXT file shown in the screenshot below will be used as basement for this R tutorial:

 

TXT file example

 

Furthermore, we have to create a data object containing the new line that we want to add to this file formatted as a character string:

my_line <- "This is a very new line that I want to add!"  # New line

 

Example: Append New Line to Text File Using write() Function

This example explains how to add a new line of text to a TXT file in R.

For this, we can apply the write function in combination with the append argument as you can see below:

write(my_line,                                            # Write new line to file
      file = "my file.txt",
      append = TRUE)

 

TXT file with new line

 

Have a look at the previously shown screenshot. It shows our updated TXT file containing an additional line of text.

 

Video & Further Resources

Have a look at the following video of my YouTube channel. I’m explaining the R code of this tutorial in the video:

 

 

Furthermore, you might read the other tutorials of my website. I have released numerous articles about related topics such as graphics in R, text elements, importing data, and character strings.

 

You have learned in this article how to add a new line of text to a TXT file in R programming. Let me know in the comments section below, if you have any further questions or comments.

 

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.

Top