Get Your Data into R: Import Data from SPSS, Stata, SAS, CSV or TXT

In this post we will show how to import data from other sources into the R workspace.

Import data from SPSS

R can import datasets from SPSS with the function read.spss() from the foreign package. Alternatively, the function spss.get() from the Hmisc package can be used. While foreign is a default package in R, the Hmisc package needs to be installed.

Here is an example:

library(foreign)
df <- read.spss("dataset.sav", use.value.label=TRUE, to.data.frame=TRUE)

df is the name of the data frame created in R, and dataset.sav is the file name of the SPSS dataset we want to import. The argument use.value.label=TRUE converts variables with value labels in SPSS into R factors, and to.data.frame=TRUE returns the result as a data frame.

Import data from Stata

To import a dataset from Stata into R, the function read.dta() from the foreign package is used. More specifically, look at the code below:

library(foreign)
df <- read.dta("dataset.dta")

df is the name of the data frame in R, and dataset.dta is the file name of the Stata dataset we want to import.

Import data from SAS

To import a dataset from SAS into R there are different methods, but the most recommended one is to first export the dataset from SAS into CSV format and then import it into R.

First, use the code below in SAS (not R) to export the data:

# run in SAS
proc export data=dataset
outfile="dataset.csv"
dbms=csv;
run;

Now that your data is exported, you can import it into R by using the code below:

df <- read.csv("dataset.csv",header=T,as.is=T)

Another way to load SAS transport files (.XPT) directly into the R environment is by using the Hmisc package:

library(Hmisc)
df <- sasxport.get("/filename.xpt") 

Import data from CSV

You can import data from a csv file into R by using the read.table() function, similarly to how we import txt files. Basically, this function reads a csv file in table format and saves it as a data frame.

This is the code we use to import a csv file into R:

df <- read.table("dataset.csv", header=TRUE, sep=",")

Often I use the function read.csv() instead, which assumes comma-separated values by default:

df <- read.csv("dataset.csv",header=T,as.is=T)

Import data from TXT

To import a text file into R, use the function read.table(). This function reads a file in table format and saves it as a data frame. The code used to import a text file is shown in the example below:

df <- read.table("dataset.txt", as.is=TRUE, header=T)

In some cases, when the values are separated by commas, I find this code useful for importing the data:

df <- read.table("dataset.txt", header=TRUE, sep=",")

Or this code:

df <- read.table("dataset.txt", header=T, strings=F)

Load data in R

Loading Rdata files in R is an easy and straightforward method. However, first we need to know how to save a data frame in R. The function used for saving a data frame is save(objectlist, file="myfile"), where objectlist is the name of your current data frame and myfile is the file name of the RDATA file you will save on your computer. The function to load the Rdata back into R is load().

Save the dataset:

save(df, file="mydata.Rdata")

Load the data in R:

load("mydata.Rdata")

Other interesting functions are ls(), which lists the objects in the current workspace, and rm(objectlist), which deletes objects from your workspace.

We finish this post here. Feel free to post a comment if you have any questions or suggestions about this post.

5 Comments

  1. TT
    Thanhtam Tran March 31, 2016

    Very interesting and useful post. I will look for your future work. It would have been nicer in some places if you had explained the meaning of some (parts) of the codes that you used, espectially in part “Assessing the predictive ability of the model”. Since you sometimes used multicodes but without explaination, so it was hard to get what their role were. Anyway, I am really enjoyed reading this post. Thanks.

    Reply
  2. H
    hadley November 23, 2015

    Or use haven: imports from SAS, Stata, and SPSS.

    Reply
    1. K
      Klodian November 23, 2015

      Thanks for your comment Hadley!

      Reply
  3. A
    Aaditya November 22, 2015

    Excellent post. Thanks! I think there’s a typo though: foreing should be foreign.

    Reply
    1. K
      Klodian November 22, 2015

      Thank you Aditya.

      Reply

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.