You are on page 1of 5

How do I read data into R? | SAMHDA https://www.datafiles.samhsa.gov/faq/how-do-i-read-data-r-n...

SAMHSA.gov
Search SAMHDA Search

SAMHDA HOME ABOUT DATA LATEST ANALYZE FAQS

How do I read data into R?


R is capable of reading data from most formats, including files created in other statistical
packages. Whether the data was prepared using Excel (in CSV, XLSX, or TXT format),
SAS, Stata, SPSS, or others, R can read and load the data into memory.

R also has two native data formats—Rdata (sometimes shortened to Rda) and Rds. These
formats are used when R objects are saved for later use. Rdata is used to save multiple R
objects, while Rds is used to save a single R object. See below for instructions on how to
read and load data into R from both file extensions.

Set the Working Directory

Before reading any data, you must set the R working directory to the location of the data.

setwd(“…”) will set the current working directory to a specific location


getwd() will print out the current directory.

> setwd("C:/mydata")

When specifying the pathname, R reads forward slashes, whereas Windows reads
backward slashes. Setting the working directory can eliminate path confusion.

Reading R Data Files

RData Files

Function: load()

> load("survey.rdata")

Or

> load("survey.rda")

Notice that the result of this function is not assigned to an object name. When R calls
load(), all of the R objects saved in the file are loaded into R. The names given to these
objects when they were originally saved will be given to them when they are loaded. The
command > ls() can be used to print out all of the objects currently loaded into R.

1 sur 5 01/03/2021 à 10:50


How do I read data into R? | SAMHDA https://www.datafiles.samhsa.gov/faq/how-do-i-read-data-r-n...

RDS Files

Function: readRDS()

> dataRDS <- readRDS("survey.rds")

The readRDS function will restore a single R object. In this example, this object was
assigned a new name of dataRDS.

Reading Delimited Data Files

Space-Delimited

Function: read.table()

Common Parameters:

Header: TRUE when first row includes variable names. The default is FALSE.
Sep: A string indicating what is separating the data. The default is " ".

> dataSPACE <-read.table("C:/mydata/survey.dat", header=TRUE, sep= " ")

With the working directory set, this is equivalent to:

> dataSPACE <-read.table("survey.dat", header=TRUE, sep= " ")

Tab-Delimited

Functions: read.table()

Common Parameters:

Header: TRUE when first row includes variable names. The default is FALSE.
Sep: A string indicating what is separating the data. The default is " ".

> dataTAB <-read.table("survey.dat", header=TRUE, sep= "\t")

Comma-Delimited

Function: read.csv()

Common Parameters:

Header: TRUE when first row includes variable names. The default is FALSE.

> dataCOMMA <-read.csv("survey.csv", header=TRUE)

2 sur 5 01/03/2021 à 10:50


How do I read data into R? | SAMHDA https://www.datafiles.samhsa.gov/faq/how-do-i-read-data-r-n...

Fixed-Width Formats

Function: read.fwf()

Common Parameters:

Header: TRUE when first row includes variable names. The default is FALSE.

> dataFW <-read.fwf("survey.txt", header=TRUE)

Reading SPSS, Stata, and SAS Data Files

The “foreign” R package can be used to read data stored as SPSS SAV files, Stata DTA
files, or SAS XPORT libraries. If foreign is not already installed on your local computer,
you can install it and load it into R with:

> install.packages(“foreign”)
> library(foreign)

SPSS

Function: read.spss()

Common Parameters:

to.data.frame: TRUE if R should treat loaded data as a data frame. The default is
FALSE.
use.value.labels: TRUE if R should convert variables with value labels into R factors
with those levels. The default is TRUE.

> dataSPSS <- read.spss("C:\mydata/survey.save", to.data.frame=TRUE)

R assumes that any value labels recorded in the SPSS file refer to factors (R’s version of a
categorical variables) and stores the labels rather than the original numbers. For
example, a variable named "gender" may be coded 0=male and 1=female and the labels
are saved in the SAV file. When R reads in the data from SPSS, the values of the variable
will be "male" and "female" instead of "0" and "1". This is the default behavior, but it can
be changed in the call to the read.spss function as:

> dataSPSS <- read.spss(file.choose(), use.value.labels=FALSE)

STATA

Function: read.data()

Common Parameters:

convert.dates: Convert STATA dates to Date class. The default is TRUE.


convert.factors: TRUE to convert value labels into factors. The default is TRUE.

3 sur 5 01/03/2021 à 10:50


How do I read data into R? | SAMHDA https://www.datafiles.samhsa.gov/faq/how-do-i-read-data-r-n...

> dataStata <- read.dta("survey.dta")

The created object is automatically a data frame. The default converts value labels into
factor levels. This can be turned off by using the following:

> dataStata <-read.dta("survey.dta", convert.factors=FALSE)

NOTE: STATA sometimes changes how it stores data files from one version to the next
and the foreign package may lag behind. If the read.dta command returns an error, then
save the data in STATA using the SAVEOLD command. This creates a DTA file saved in a
previous version of STATA that read.dta may be more likely to recognize.

SAS

Function: read.xport()

> dataSAS <- read.xport("C:/mydata/survey")

The function returns a data frame if there is a single dataset in the library or a list of data
frames if there are multiple datasets.

Reading Excel Data Files (XLSX or XLS)

EXCEL

Function: read_excel()

Common Parameters:

Sheet: The name of the sheet or its location number.

It may be easier to use Excel to save individual sheets as CSV files and then read the CSV
files into R. However, reading the XLSX and XLS extensions is possible in R:

> install.packages(“readxl”)
> library(readxl)
> dataEXCEL <- read_excel(“survey.xlsx”, sheet = 1)
> dataEXCEL <- read_excel(“survey.xlsx”, sheet = “sheetname”)

This creates an R tibble (the newer version of an R dataframe). If you are more
comfortable with R dataframes, please use:

> dfEXCEL <- as.data.frame(dataEXCEL)

« Back To FAQs

4 sur 5 01/03/2021 à 10:50


How do I read data into R? | SAMHDA https://www.datafiles.samhsa.gov/faq/how-do-i-read-data-r-n...

SAMHSA.gov Homepage Site Map Viewers & Plugins Accessibility Privacy


Disclaimer FOIA Plain Language Nondiscrimination Notice

SAMHSA's mission is to reduce the impact of


substance abuse and mental illness on
America's communities.

5600 Fishers Ln, Rockville, MD 20857


1-877-SAMHSA-7 (1-877-726-4727)

5 sur 5 01/03/2021 à 10:50

You might also like