Exporting Data From R to CSV, Text, SAS, SPSS or Stata

1. Objective

In this Exporting Data from R tutorial, we will learn how to export data from R to CSV file. We will also learn ways of writing data from R to text files and how can we export data from R to excel. In the further tutorial, we will also understand exporting dataset from R in SAS/SPSS/Stat using “Foreign” package.

Exporting Data From R to CSV, Text, SAS, SPSS or Stata

Exporting Data From R to CSV, Text, SAS, SPSS or Stata

2. Introduction to Exporting data from R

So far we have seen how data can be imported in R. Now we are going to learn how we can export data from R.
Exporting results from R is usually a less contentious task. We can export data from R to CSV file or Text file or Excel sheet. We can also export R data to SAS or SPSS or Stata.

i. 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 a special case of write.table().The command can be used as follows:
>write.table(file_name, file="clipboard", sep="\t", row.names=FALSE)
The write.table() command is used with the following arguments:file=”clipboard”
i. file=”clipboard”
ii. sep=”\t”
iii. row.names=FALSE
Here separator is tab character (\t). This means using above command, tabular data can be written to the clipboard.

ii. Exporting R data to an Excel Spreadsheet

Below commands are used to export R data to an excel sheet:

library(xlsx)
write.xlsx(mydata, "c:/mydata.xlsx") 

iii. Exporting data from R to SPSS

For exporting R data to SPSS or SAS or Stata, you first need to load the “Foreign” package. Then you can export data from R in SAS or SPSS or Stata.
Below is the way to write out text datafile and a SPSS program to read it:

library(foreign)
write.foreign(mydata, "c:/mydata.txt", "c:/mydata.sps", package="SPSS")

iv. Exporting data from R to SAS

Below is the way to write out text datafile and a SAS program to read it:

 library(foreign)
write.foreign(mydata, "c:/mydata.txt", "c:/mydata.sas", package="SAS")

v. Exporting R data to Stata

We use below method to export data frame to Stata binary format:

library(foreign)
write.dta(mydata, "c:/mydata.dta")

R Quiz

3. Saving Work in R

Let us now understand how we can save our work in R programming language.
The created data items and results will need to save to the disk in order to work on them later. Several methods can use to save the work. The most popular ones are:

i. Saving to a disk

You can save individual objects, or indeed all the objects, to disk at any time by using the save()command. Below is the way to use save() command.
>save(list, file = 'filename')
Now we are going to see variations of save command.
i. To save the object by name and separating values using commas, we use below command:
>save(bf, bf.lm, bf.beta, file = 'Desktop/file_name.RData')
ii. To save the objects by referring to a list of names created by some other means, we use below command:
>save(list = ls(pattern = '^bf'), file = 'Desktop/file_name.RData')
In both cases, the output file is saved to the desktop folder rather than the default.
iii. To save everything specified by the ls() command, w use below command:
>save(list = ls(all=TRUE), file = ‘filename’)
iv. To save the entire current session, we use below command:
>save.image(file = ‘filename’)
This example is a special command that allows you to save everything with less typing. This is essentially what happens when the user saves the workspace while quitting R. If no file name is specified, we use the default; the file name defaults to ..RData.

ii. Saving to text files

For saving data to disk as text files while doing Data Analytics:
i. Data can transfer out of R by using the write.table(), write.csv(), and cat() commands.
ii. The command which uses depends on that data to save to the disk.
iii. For a single vector of values, the write() or cat() command can use.
iv. For multiple column items containing several variables, the write.table() or write.csv() command can use.

4. Conclusion – Exporting Data From R

Hence, in this tutorial of exporting data from R, we discussed several steps as the export table from CSV file, data from R to excel sheet, SPSS, Stata, SAS. Moreover, we discussed how to save work in R Programming. Still, if you have any doubt regarding exporting Data in R, ask in the comment tab.

Reference for R

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.