Professional Documents
Culture Documents
SAMHSA.gov
Search SAMHDA Search
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.
Before reading any data, you must set the R working directory to the location of the data.
> setwd("C:/mydata")
When specifying the pathname, R reads forward slashes, whereas Windows reads
backward slashes. Setting the working directory can eliminate path confusion.
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.
RDS Files
Function: readRDS()
The readRDS function will restore a single R object. In this example, this object was
assigned a new name of dataRDS.
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 " ".
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 " ".
Comma-Delimited
Function: read.csv()
Common Parameters:
Header: TRUE when first row includes variable names. The default is FALSE.
Fixed-Width Formats
Function: read.fwf()
Common Parameters:
Header: TRUE when first row includes variable names. The default is FALSE.
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.
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:
STATA
Function: read.data()
Common Parameters:
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:
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()
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.
EXCEL
Function: read_excel()
Common Parameters:
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:
« Back To FAQs