write Function in R (2 Examples)


In this R tutorial you’ll learn how to write data to files using the write() function.

The article will consist of the following:

Here’s the step-by-step process!

Definition & Basic R Syntax of write Function


Definition: The write R function writes data to files.


Basic R Syntax: In the following, you can find the basic R programming syntax of the write function.

write(data, "")                           # Basic R syntax of write function

In the following, I’ll show two examples for the application of the write function in the R programming language.


Creating Example Data

The following data will be used as basement for this R tutorial:

data <- matrix(1:15, ncol = 3)            # Create example data matrix
data                                      # Print example data
#      [,1] [,2] [,3]
# [1,]    1    6   11
# [2,]    2    7   12
# [3,]    3    8   13
# [4,]    4    9   14
# [5,]    5   10   15

The previous output of the RStudio console shows that our example data is a numeric matrix consisting of five rows and three columns.


Example 1: Write Data Matrix to RStudio Console

In Example 1, I’ll explain how to print a matrix to the RStudio console using the write() function in R. Have a look at the following R code:

write(data, "", sep = "\t")               # Apply write function
# 1 	2 	3	4       5
# 6 	7 	8 	9 	10
# 11	12	13	14	15

Within the write function, we had to specify the name of our data (i.e. data), the name of our file (i.e. “” since we were writing the data to the console), and a separator (i.e. “\t”) to print our data in a prettier format.


Example 2: Write Data Matrix to New Window

In Example 2, I’ll explain how to open a new window in RStudio and print our data matrix to this window. First, we have to specify a temporary file name:

data_file <- tempfile("data")             # Open temporary file

Then, we have to apply the write function:

write(data, data_file)                    # Apply write function

Then, we have to open a new window in RStudio that shows our data:

if(interactive()) file.show(data_file)    # Open new window and write file

And finally, we have to unlink the temporarily opened file connection:

unlink(data_file)                         # Unlink temporary file

After running the previous R code, a new window showing our example data should be opened in your RStudio.


Video & Further Resources

Do you need further info on the topics of this tutorial? Then you might want to have a look at the following video of my YouTube channel. I illustrate the R programming syntax of this article in the video:


The YouTube video will be added soon.


In addition, you could read the other tutorials of my website:


Summary: In this tutorial you learned how to apply the write() function in R. Let me know in the comments, in case you have further questions.


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.