DataScience+ An online community for showcasing R & Python tutorials. It operates as a networking platform for data scientists to promote their talent and get hired. Our mission is to empower data scientists by bridging the gap between talent and opportunity.
Getting Data

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 package foreign. Alternatively, the function spss.get() from Hmisc package can be used. While foreign is a default package in R, the Hmisc package need to be installed.

Here is an example:

df <- read.spss("dataset.sav", use.value.label=TRUE,

df is the name of data frame I created in R, and dataset.sav is the file name of SPSS dataset we want to import, and use.value.label=TRUE to convert variables with value labels in SPSS into R factors, and to make as data frame.

Import data from Stata

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

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

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

Import data from SAS

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

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

# run in SAS
proc export data=dataset

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

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

Another way to upload SAS files (.XPT) direct to R environment is by using Hmisc package.

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

Import data from CSV

You can import data from csv into R by using read.table() function similarly as we used with importing 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 csv file into R

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

Often I use the function read.csv() to import csv file into R

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

Import data from TXT

To import 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 text file is in example below.

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

In some cases 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

To load Rdata in R is easy and straightforward method. However, first we need to know how to save the dataframe in R. The function used for saving the dataframe is save(objectlist, file="myfile"), where objectlist is the name of your current dataframe and myfile is the filename of RDATA you will save on your computer. The function to upload the Rdata in R is load().

Save the dataset:

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

Load data in R:


Other interesting functions are ls() which list the objects in the current workspace, rm(objectlist) which delete objects in your workspace.

We finish this post here. Feel free to post comment if you have any question or suggestion about this post.