Skip to main content
v2026.11,858 entries · CC-BY 4.0

How to Load Data into R: CSV, Excel, SPSS, Stata and SAS Files

Load data into R with read.csv, read_excel and haven: the right function per file type, documented defaults, and fixes for separator, encoding and NA errors.

Written and maintained by CASRAI Editorial Board

Last updated

To load data into R, call the reader function that matches the file format and assign the result to an object: read.csv() for comma-separated files, read.csv2() for semicolon-separated files with a decimal comma, read.delim() for tab-separated files, readxl::read_excel() for Excel workbooks, and haven::read_sav(), read_dta() or read_sas() for SPSS, Stata and SAS files. Most failed imports come from three settings: the separator, the decimal mark and the encoding. This guide ties each file format to its documented function and defaults, then shows how to check what R actually read.

What it is used for in research

Every analysis in R starts with an import step. Research data usually arrives as a spreadsheet export (CSV or Excel), a plain-text table such as a TSV file, or a file saved by another statistics package. R reads each of these into a data frame that later steps, such as summary statistics in R or hypothesis testing with R, can use. If you are new to the environment itself, start with what R is used for and how to use RStudio.

Which function reads which file

File type Base R Package function Documented defaults or notes
CSV, comma-separated read.csv() readr::read_csv() read.csv(): header = TRUE, sep = ",", dec = "."
CSV, semicolon-separated, decimal comma read.csv2() readr::read_delim() read.csv2(): sep = ";", dec = ","
Tab-separated read.delim() readr::read_tsv() read.delim(): sep = "\t", dec = "."
Other delimited text read.table() readr::read_delim() read_delim() covers delimited files; CSV and TSV are special cases
Excel .xls and .xlsx none in base R readxl::read_excel() Detects the format from the file extension; no external dependencies
SPSS .sav foreign::read.spss() haven::read_sav() Both read SPSS save or export files
Stata .dta none in base R haven::read_dta() haven reads Stata files up to version 15; for Stata 13 and later the R manual also points to readstata13
SAS .sas7bdat none in base R haven::read_sas() Reads .sas7bdat data and .sas7bcat catalog files

The separator, decimal mark and header defaults above are those in the R documentation for read.table and its relatives.

Install or access (official source only)

The base R readers (read.csv, read.csv2, read.delim, read.table) ship with R in the utils package, so nothing needs installing. The foreign package, which the R Data Import/Export manual recommends for files produced by other statistical systems, is also described there. The tidyverse readers install from CRAN inside R:

  1. install.packages("readr") for CSV, TSV and other delimited text (or install.packages("tidyverse") to get readr with the rest of the tidyverse).
  2. install.packages("readxl") for Excel workbooks.
  3. install.packages("haven") for SPSS, Stata and SAS files.

Install each package once, then load it with library() in every new session.

Step-by-step: load a data file

1. Load a CSV file

  1. Put the file path in quotes. The R manual recommends specifying the header argument explicitly, so write it out: data <- read.csv("survey.csv", header = TRUE).
  2. For a semicolon-separated file from a locale where the comma is the decimal mark, use read.csv2("survey.csv", header = TRUE). The manual notes read.csv2 is also the function for semicolon-separated spreadsheet exports.
  3. For a tab-separated file, use read.delim("survey.tsv", header = TRUE).
  4. With readr, run library(readr) and then data <- read_csv("survey.csv"), or use read_tsv() for tab-separated files.

2. Load an Excel workbook

  1. Run library(readxl).
  2. List the sheet names with excel_sheets("workbook.xlsx").
  3. Read a sheet by name or by number: data <- read_excel("workbook.xlsx", sheet = "Sheet1").
  4. To read only part of a sheet, pass an Excel-style cell range through the range argument, for example range = "B1:D5".

3. Load SPSS, Stata or SAS files

  1. Run library(haven).
  2. Use read_sav("study.sav") for SPSS, read_dta("study.dta") for Stata or read_sas("study.sas7bdat") for SAS.
  3. haven keeps value labels and special missing values in its labelled() class, so variable labels from the original package survive the import.

Output and how to read it

A successful import returns a data frame. Check it before analysing: print the first rows with head(data) and look at each column’s type with str(data). Numbers read as text usually mean a stray character, the wrong decimal mark or a non-standard missing-value code.

The readr functions guess column types when you give no column specification. Retrieve the guess with spec(data) and list any values readr could not parse as expected with problems(data). In readr’s second edition, read_csv() calls vroom::vroom() by default.

Base R names are adjusted with make.names() when needed (the check.names argument), so a spreadsheet header such as “Age (years)” can arrive as Age..years..

Troubleshooting

All the data lands in one column

The separator is wrong. read.csv() assumes commas; a semicolon-separated file needs read.csv2(), or sep = ";". A tab-separated file needs read.delim().

Decimal numbers are read as text

In locales where the comma is the decimal mark, the R manual points to read.csv2() and read.delim2(). Alternatively, set dec = "," explicitly.

Missing values are not recognised as NA

By default a file is assumed to use the string NA for missing values. If your file uses another code, such as a dash or -99, name it with the na.strings argument.

Accented characters look wrong

Declare the encoding. The documentation says that if the input is known to be UTF-8 or Latin-1, use the encoding argument to declare that; it is not used to re-encode the input. The separate fileEncoding argument declares the encoding used on the file.

The first row is read as data, or the header is dropped

Set header = TRUE or header = FALSE explicitly, as the manual recommends, instead of relying on the function’s guess.

Column types are not what you expected

Call spec() and problems() on the readr result, then supply a column specification. In base R, stringsAsFactors defaults to FALSE, so text columns stay as character vectors unless you ask for factors.

Free alternatives: base R and tidyverse readers

All of these are free, open-source R packages or part of R itself.

Task Base R or recommended package Tidyverse package
Delimited text (CSV, TSV) read.csv, read.csv2, read.delim readr: read_csv, read_delim, read_tsv
Excel (.xls, .xlsx) none in base R; the R manual describes the readxl package readxl: read_excel
SPSS foreign (recommended package): read.spss haven: read_sav
Stata none in base R; the R manual points to the readstata13 package for Stata 13 and later haven: read_dta
SAS none in base R haven: read_sas

If you are choosing which statistics package to work in, see free statistical software for researchers.

FAQ

How do I read a semicolon-separated CSV file in R?

Use read.csv2(), which sets sep = ";" and dec = ",". With readr, use read_delim() and set the delimiter.

How do I load an Excel file into R?

Install the readxl package, load it, and call read_excel() with the file path. It reads both .xls and .xlsx files and detects the format from the extension. Use excel_sheets() to list sheets.

Can R read SPSS, Stata and SAS files?

Yes. The haven package provides read_sav(), read_dta() and read_sas(). The foreign package also reads SPSS files with read.spss().

Sources

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about How to Load Data into R: CSV, Excel, SPSS, Stata and SAS Files

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →

Ask CASRAI · Regulatory Radar

Research-admin question? Get an answer that links its sources.

An AI assistant specialized in research administration. Every answer links its sources to check before you act. 2 questions free, no account. $29/month after.

  • Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.
  • Every answer numbers its sources and links each one, so you can check the source yourself.