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:
install.packages("readr")for CSV, TSV and other delimited text (orinstall.packages("tidyverse")to get readr with the rest of the tidyverse).install.packages("readxl")for Excel workbooks.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
- Put the file path in quotes. The R manual recommends specifying the
headerargument explicitly, so write it out:data <- read.csv("survey.csv", header = TRUE). - For a semicolon-separated file from a locale where the comma is the decimal mark, use
read.csv2("survey.csv", header = TRUE). The manual notesread.csv2is also the function for semicolon-separated spreadsheet exports. - For a tab-separated file, use
read.delim("survey.tsv", header = TRUE). - With readr, run
library(readr)and thendata <- read_csv("survey.csv"), or useread_tsv()for tab-separated files.
2. Load an Excel workbook
- Run
library(readxl). - List the sheet names with
excel_sheets("workbook.xlsx"). - Read a sheet by name or by number:
data <- read_excel("workbook.xlsx", sheet = "Sheet1"). - To read only part of a sheet, pass an Excel-style cell range through the
rangeargument, for examplerange = "B1:D5".
3. Load SPSS, Stata or SAS files
- Run
library(haven). - Use
read_sav("study.sav")for SPSS,read_dta("study.dta")for Stata orread_sas("study.sas7bdat")for SAS. - 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
- R Core Team, Data Input (read.table, read.csv, read.csv2, read.delim), R documentation, checked 29 October 2026.
- R Core Team, R Data Import/Export, CRAN, checked 29 October 2026.
- readr documentation, readr.tidyverse.org, checked 29 October 2026.
- readxl documentation, readxl.tidyverse.org, checked 29 October 2026.
- haven documentation, haven.tidyverse.org, checked 29 October 2026.








