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 for 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

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.

a. 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.

b. 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") 

c. 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")

d. 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")

e. Exporting R data to Stata

Below method is used to export data frame to Stata binary format:

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

3. Saving Your 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 be saved to the disk in order to work on them later. Several methods can be used to save the work. The most popular ones are:

a. 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, below command is used:

>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, below command is used:

>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, below command is used:

>save(list = ls(all=TRUE), file = ‘filename’)

iv. To save the entire current session, below command is used:

>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, the default is used; the file name defaults to ..RData.

b. Saving to text files

For saving data to disk as text files while doing Data Analytics:

i. Data can be transferred out of R by using the write.table(), write.csv(), and cat() commands.

ii. The command used depends on that data to be saved to the disk.

iii. For a single vector of values, the write() or cat() command can be used.

iv. For multiple column items containing several variables, the write.table() or write.csv() command can be used.

Source:

https://en.wikibooks.org/wiki/R_Programming/Importing_and_exporting_data

Leave a comment

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