The Complete Guide to Exporting Data in R

Deepanshu Bhalla Add Comment

This tutorial explains how to export data into various formats using R.

Exporting Data using R
Exporting Data with R

1. Writing Data to CSV file in R

The following code uses the write.csv() function to save the "mydata" dataframe as a CSV file named "test.csv". Make sure to use forward slashes (/) in file paths.

write.csv(mydata,"C:/Users/Deepanshu/Desktop/test.csv")

2. Writing Data to Text file in R

The following code uses the write.table() function to save the "mydata" dataframe as a tab-delimited text file named "test.txt".

write.table(mydata, "C:/Users/Deepanshu/Desktop/test.txt", sep="\t")

3. Writing Data to an Excel file in R

To write data to an Excel file in R, you can use the the openxlsx package.

Install the openxlsx package

install.packages("openxlsx")

The write.xlsx function from openxlsx package writes a data.frame to an xlsx file.

library(openxlsx)
write.xlsx(mydata, file = "test.xlsx")

4. Writing Data to SAS file in R

To write data to a SAS file in R, you can use the haven package. This package has various functions to export or import SAS files.

Install the haven package

install.packages("haven")

The SAS transport format (.xpt) is a open format which is generally used by regulatory bodies. The write_xpt function from haven package writes a data.frame to SAS file.

library(haven)
tmp <- tempfile(fileext = ".xpt")
write_xpt(mydata, tmp)

read_xpt(tmp) reads the SAS transport format file.

5. Writing Data to SPSS file in R

Install the package once

install.packages("haven")

The write_sav function from haven package writes a data.frame to SPSS file.

library(haven)
tmp <- tempfile(fileext = ".sav")
write_sav(mydata, tmp)
Related Posts
Spread the Word!
Share
About Author:
Deepanshu Bhalla

Deepanshu founded ListenData with a simple objective - Make analytics easy to understand and follow. He has over 10 years of experience in data science. During his tenure, he worked with global clients in various domains like Banking, Insurance, Private Equity, Telecom and HR.

Post Comment 0 Response to "The Complete Guide to Exporting Data in R"
Next → ← Prev