• Category
  • >R Programming

Exporting Data from R

  • Lalit Salunkhe
  • Oct 08, 2020
Exporting Data from R title banner

Importing data in R is surely important for the user. However, exporting data from R to other platforms is equally important as well. You may want to export the data from R workspace into an excel file, or a CSV, or a Text file, or a PDF (in case you are creating a fancy report that needs to be sent to your boss). It is as straightforward to export data from R as it is to import it into R. 

 

Through this article, we will walk through the processes and steps involved in exporting the data from R workspace to other platforms. We will also try to cover different functions from packages such as readr and xlsx that are associated with the different format options for the excel files.

 

Through this article, we will walk you through how to export files from R to - 

 

  • Exporting into Text/CSV

  • Exporting into Excel 

  • Exporting into R Objects/R data files

 

Well, if you haven’t gone through yet, we would like you to read our article on Importing Data into R.


 

Exporting Data into Text/CSV

 

Well, exporting data into a text or a CSV file, is the most popular and indeed common way of data export. Not only because most of the software supports the option to export data into Text or CSV but also because these files are supported by almost every software/programming language that exists. 

 

There are two ways of exporting data into text files through R. One is using the base R functions and another one is using the functions from the readr package to export data into text/CSV format.

 

Using Built-in Functions

 

There is a popular built-in R function named write.table() which can do the task of exporting the data into text files from R workspace. The function has two other special cases namely write.csv() and write.delim() out of which the first one helps to export the data into CSV format and the second one is adjusted way of write.table() where default delimiters can be adjusted.

 

Let us see an example here which will clear the air out about these functions. First of all, let us create a data frame that we would love to export into a text/CSV file.


Creating a data frame which can be exported as a text file.

Creating a data frame to export



To export this data frame into a CSV file, we can use the write.csv() function. See the example below:


This code shows how to export the data from R Workspace into csv with different options.

Exporting data from R into CSV


In this example, you could see the write.csv() function exports the data frame into a new CSV file named “d_frame_export”. The thing you should keep in mind is about the file extension. You need to add the .csv extension for this file to be exported as a CSV file. The file gets stored at the default working directory.

 

You can also store this file at a location of your choice. Which is what exactly we did in the second example in the code above. The third example emphasises the use of additional arguments that can be used under the write.csv() function. Ex. row.names = FALSE will allow you to eliminate the row names from the exported data file. We can export this data frame as a text file using write.table() function.


 This image shows how the write.table() function allows you to export the data into a delimited text file.

Exporting data into a text file


Using Functions from readr Package

 

The functions under the readr package are similar to the functions available under base R. There is a minor difference in their look (write.csv() from base R and write_csv() from readr do the same stuff). Besides the functions developed under the readr package are using path = argument instead of file = to specify the path where the file needs to export. The functions from the readr package exclude the row names by default.

 

Also, the most important advantages for the functions from readr package is, they are two times faster when it comes to the execution of the functions. See an example below where we try to export the same data frame into CSV and text file using functions built under readr package.


This image shows how the functions from the readr package work to export a data file into csv, and text file.

Exporting data into CSV/text file using functions from the readr package


This code shows different ways of exporting the data into either CSV or text file using multiple alternatives. The UTF-8 is a special type of CSV file which has a different format than the usual CSV file and the excel recognizes such file. It is a CSV file with a bit of different formatting.


 

Exporting Data into Excel

 

Now, to export data into Excel from R workspace, the best bait you could put on is the writexl package. This package allows you to export data as an Excel file into xlsx format. It may be looking outdated at this moment but believe me, the functions do their task with precision. Besides, we always have the compatibility of new excel to be able to connect with the older versions.

 

See code below that explains how to export the data into an excel file using functions from the writexl package.


This image shows the code which allows you to export the data frame into an excel file using functions from writexl package.

Exporting data frame into excel files using functions from writexl package


Now, here if you see, the functions from writexl packages are similar with those from the xlsx packages with only difference in their looks (xlsx package have write.xlsx() and here we have write_xlsx()). 

 

Moreover, as shown in the previous example the write_excel_csv() function allows you to store a file into UTF-8 encoded CSV file which is recognized by Microsoft Excel. This gives us another way to export our data into excel format.

 

Well, one thing to note here is, before starting to use the packages you should install them first into your workspace. Our article on Importing Data into R will help you understand how to import data into your workspace and how to access the same.


 

Exporting Data into R Objects

 

There might come situations where you wanted to share the data from R as Objects and share those with your colleagues through different systems so that they can use it right away into their R workspace. These objects are of two types .rda/.RData and .rds.

 

The .rda and.RData is similar and they can be used to store some or all objects, functions from your global environment. Whereas when you want to store single objects from your workspace (data frame, a newly developed statistical model), it is better to use the .rds file. Well, you can also use the.RData, .rda to save the single objects. 

 

But the .rds has real benefit lying within the fact that it allows you to store the object with a name and you can assign the same to another object using assignment operator and access. On the other hand when you save a file as.RData or .rda, it stores the object and its name as well which means you can’t access a single object with a different alias.

 

Let’s see some examples of exporting data into R Objects.

 


This image shows different ways of exporting data into an R object.

Exporting data as an R Object



 

Summary

 

  • Exporting data is as important as importing the data into R workspace

  • To import data into csv, we can use the built-in R functions such as write.table(), write.csv() or a more faster and simplified readr package which has functions such as write_csv(), write_delim()

  • Functions from readr package are twice speedy when it comes towards the execution speed.

  • The functions from readr package also exclude the row names for a data frame by default.

  • You can export a file at a different location by specifying the path to these functions. Moreover, you can store the text files with different delimiters.

  • Writexl package provides some useful functions that can help you export data into an excel file from R workspace.

  • R data objects can be important to export your data to when you have to share it with your colleagues or the user so that they can use it in their R Workspace. (Visit our exclusive section "R Programming" to learn prime functions in R)

 

This article ends here. We will come up with a new and interesting article on this list for you. Until then, Stay safe!

Latest Comments

  • brenwright30

    May 11, 2024

    THIS IS HOW YOU CAN RECOVER YOUR LOST CRYPTO? Are you a victim of Investment, BTC, Forex, NFT, Credit card, etc Scam? Do you want to investigate a cheating spouse? Do you desire credit repair (all bureaus)? Contact Hacker Steve (Funds Recovery agent) asap to get started. He specializes in all cases of ethical hacking, cryptocurrency, fake investment schemes, recovery scam, credit repair, stolen account, etc. Stay safe out there! Hackersteve911@gmail.com https://hackersteve.great-site.net/