Export Data in R Programming


May 19, 2022, Learn eTutorial
1388

In this tutorial, you will learn about how to export (write) data to any new file format such as txt, CSV, etc. The data scientist usually stores data in Excel Spreadsheets. In the R programming language, there are several R packages such as XLConnect, xlsx, RExcel, gdata, RODBC, etc to access the data from Excel Spreadsheets. The R users or programmers save the spreadsheets (data) mostly in CSV format/files to take advantage of R‘s built-in functionalities to manipulate the data. So through this tutorial, you will learn the read (import) and write (export) operations from .csv files in R language.

How to export data from R to CSV file?

In the below-given screenshot, you can view that only the folder R programs contain only a single CSV file name 3-1homprices. The data you created or manipulated can be stored using the write.csv() method.
 

How to export data in R?

Consider the below given piece of code


write.csv(df1, file ="C:\\Users\\Desktop\\R\\R Pgms\\3-1homeprices1.csv")
 

The write.csv() function with the name of the data frame (df1) to be saved is given as the first argument followed by the path or location where the data needs to be stored.

After executing the above code you can view a new file 3-1homeprices1 with a .csv extension created inside the same folder.(The user can store the file based on his wish)

The screenshot below shows the exported file.

How to export data in R?

How to export /write data from R as text file?

Earlier we imported a CSV file named  3-1homeprices  using read.csv() function. Now let us export this 3-1homeprices  as a .txt file with tab-separated.The write.table() function is used 


write.table(df1,file ="C:\\Users\\Desktop\\R\\R Pgms\\3-1homeprices1.txt",sep = "\t",row.names=FALSE)
 

With arguments such as the name of the data frame (df1), the path where you need to store the data frame with a new file name with .txt as an extension, then specify the separator to separate the data with a space or tab.

How to export data in R?

The code after execution you can view the newly created txt file at the specified name. In our example, a new file named 3-1homeprices1 gets created and stored in a folder for later use as shown below screenshot.

How to export data in R?

Once after opening the .txt file you can view the below data