How to Apply the write() Function in R (2 Examples)

In this R programming tutorial you’ll learn how to apply the write() function.

Creating Example Data

my_df <- matrix(1:20, nrow = 4)         # Creating matrix in R
my_df                                   # Returning example matrix
#      [,1] [,2] [,3] [,4] [,5]
# [1,]    1    5    9   13   17
# [2,]    2    6   10   14   18
# [3,]    3    7   11   15   19
# [4,]    4    8   12   16   20

Example 1: Using write() Function to Return Matrix to Console

write(my_df,                            # Applying write() function
      "",
      sep = "t")
# 1 	2	  3	  4   5
# 6	  7 	8 	9	  10
# 11	12	13	14	15
# 16	17	18	19	20

Example 2: Writing Matrix to New R Window

my_file <- tempfile("data")             # Opening temporary file
write(my_df,                            # Applying write() function
      my_file)
if(interactive()) file.show(my_file)    # Writing file to new window
unlink(my_file)                         # Unlinking temporary file

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.
You need to agree with the terms to proceed

Menu
Top