Exporting Data from R to Other File Formats – Guide for Data Science Aspirants!
Previously, we explored the process of importing data in R, now, in this tutorial, we will learn the steps of exporting data from R programming to CSV, Excel, SPSS, SAS and Stata. Lastly, we will understand the process of saving work in R.
Let’s quickly start.
Introduction to Exporting Data from R
Exporting results from R programming is usually a less contentious task.
1. Exporting table data to a CSV file
Exporting results from R to other applications in the CSV format is just as convenient as importing data into R by using CSV files.
To create a CSV file, the write.csv()function can be used. Just as the read.csv() function is a special case of read.table(), write.csv() is also a special case of write.table(). The command can be used as follows:
> #Author DataFlair > data <- data.frame(x1 = c(1, 2, 3, 4), + x2 = c(5, 6, 7, 8), + x3 = c(9, 10, 11, 12)) > write.table(data, file = "data.csv", + sep="\t", row.names=FALSE)
Here separator is tab character (\t). This means using the above command, tabular data can be written to the clipboard.
2. Exporting R data to an Excel Spreadsheet
Below commands are used to export R data to an excel sheet:
library(xlsx) write.xlsx(data, "/home/dataflair/data.xlsx")
3. Exporting data from R to SPSS
In order to export the data from R to SPSS, you will require the “Foreign” package. Then, you can export data from R to SAS or SPSS or Stata.
In order to write the data file, that can be interpreted by the SPSS, we write the following lines of code:
> #Author DataFlair > library(foreign) > write.foreign(data, "/home/dataflair/data.txt", "/home/dataflair/data.sps", package="SPSS")
Wait! Have you checked – Binomial and Poisson Distribution in R
4. Exporting data from R to SAS
Below is the way to write out text data file and a SAS program to read it:
> #Author DataFlair > library(foreign) > write.foreign(data, "/home/dataflair/data.txt", "/home/dataflair/data.sas", package="SAS")
5. Exporting R data to Stata
We use the below method to export data frame to Stata binary format:
> library(foreign) > write.dta(data, "/home/dataflair/data.dta")
Saving Work in R
Let us now understand how we can save our work in the R programming language. For saving data to disk as text files while performing Data Analytics:
- The command which we will use depends on the format of data to save to the disk. Transfer the data out of R by using the write.table(), write.csv(), and cat() commands.
- For a single vector of values, use the write() or cat() command. For multiple column items containing several variables, use the write.table() or write.csv() command.
We learned several steps to export table data to CSV file, R data to excel sheet, SPSS, Stata and SAS in this tutorial. We also discussed how to save work in R Programming.
Next, master the concept of Predictive and Descriptive Analytics in R
Still, if you have any doubt regarding the tutorial, ask in the comment section.