Skip to main content
v2026.11,858 entries · CC-BY 4.0
LAC HealthMedical & Research SuppliesReagents, PPE & instruments — chain-of-custody documented.Fast, traceable sourcing built for regulated research environments, from bench consumables to instrumentation.Shop lac.us CodeCASRAIlac.us

How to Import Data in R: read.csv, readr and haven

Import CSV, tab-delimited and SPSS data in R with read.csv, read_csv and read_sav, check the result, and export it again.

Written and maintained by CASRAI Editorial Board

Last updated

Who this is for: researchers who have a data file (CSV, tab-delimited text or an SPSS file) and need it as a data frame in R, and who want to check the import worked. Short answer: use read.csv() from base R, read_csv() from the readr package, or read_sav() from the haven package for SPSS files. All three return a table you assign to a name with <-.

What importing data is used for in research

Almost every analysis in R begins by reading a file into a data frame. The official manual, R Data Import/Export (covering R 4.6.2, dated 2026-10-29), documents the base functions and the packages for other formats. The choice of function decides how column types, missing-value strings and character data are handled, which is why the import step is worth checking before any statistics are run. For what a data frame is once it is loaded, see data frames in R.

Install or access (official sources only)

  • Base R (read.table, read.csv, write.table, write.csv) ships with R from CRAN (cran.r-project.org); nothing extra to install.
  • readr: install.packages("readr"), or install.packages("tidyverse") to get it with the rest of the tidyverse (readr.tidyverse.org).
  • haven: install.packages("haven"), or via the tidyverse install (haven.tidyverse.org).
  • foreign: the package the CRAN manual documents for reading SPSS files with read.spss (section 3.1 of the manual).

Step by step: import a CSV or delimited file

Import data in R with base R

  1. Call read.table() (or its CSV wrapper read.csv()) with the file path and assign the result: df <- read.csv("mydata.csv").
  2. Set header explicitly. The manual says: “We recommend that you specify the header argument explicitly.”
  3. For tab-separated text, set the separator: read.table("mydata.txt", header = TRUE, sep = " ").
  4. If the file marks missing values with a string such as “NA” or “-“, list it in na.strings.
  5. To fix column types, pass colClasses; the manual notes this is faster and uses less memory than letting R guess. Use as.is = TRUE to stop character columns being converted to factors.

Import with readr

  1. Install and load readr: install.packages("readr"), then library(readr).
  2. Read the file: df <- read_csv("mydata.csv"). readr’s own example is read_csv(readr_example("chickens.csv")).
  3. readr prints the column types it guessed. Run spec(df) to retrieve the full guessed specification, then copy and adjust it into an explicit call if a guess is wrong.

Other readr readers named on the package site: read_tsv (tab-separated), read_csv2 (semicolon-separated, comma decimal), read_delim (general delimiter), read_fwf (fixed width), read_table (whitespace-separated) and read_log (web logs).

How to import data from SPSS into R

Direct answer: use read_sav("file.sav") from haven, or read.spss() from the foreign package. Haven also has read_por() for .por files.

  1. install.packages("haven"), then library(haven).
  2. df <- read_sav("file.sav").
  3. Haven turns SPSS value labels into the labelled() class and preserves special missing values. Convert a labelled column to an ordinary factor with as_factor().

The CRAN manual (section 3.1) states that read.spss “can read files created by the ‘save’ and ‘export’ commands in SPSS.” If you are weighing the two tools for a project, see SPSS vs R.

Output and how to read it: check the import

An import that runs without an error can still be wrong. After reading a file, check these in order:

  1. Look at the first rows with head(df) and the structure with str(df): are the column names right and is each column the type you expect (numeric, character, factor)?
  2. With readr, read the printed column specification, or call spec(df).
  3. If numbers arrive as text, look for a stray non-numeric string and add it to na.strings, or declare the type with colClasses.
  4. If the first data row became column names (or the reverse), set header explicitly.

How to export data out of R

Direct answer: write.csv(df, "out.csv") writes a data frame to a CSV file. The manual describes write.table as writing a data frame with row and column labels, and write.csv as a wrapper that “provides appropriate defaults”. For SPSS, haven’s write_sav(df, "file.sav") writes a .sav file. A round trip (write, then read back and compare with str()) is a quick way to confirm nothing changed type. For choosing a file format, see CSV vs XLSX.

What does .data do in R?

In tidyverse code, .data is a pronoun from the rlang package. The rlang documentation says .data retrieves data-variables from the data frame (and .env retrieves variables from the environment), so that the lookup is explicit, as in mtcars %>% mutate(disp = .data$disp * .env$m). It is “only a pronoun, it is not a real data frame”. It is not an import function and has no role in reading files.

Troubleshooting

  • Everything is in one column: the separator is wrong. Set sep in base R, or use read_csv2 for semicolon-separated files with comma decimals, or read_delim for another delimiter.
  • Text appears as factors: pass as.is = TRUE in base R.
  • Wrong column types: set colClasses (base R) or copy the output of spec() into an explicit specification (readr).
  • SPSS labels look odd: use as_factor() on haven labelled columns.

Free alternatives: the main import functions compared

Every option below is free to install from CRAN or the tidyverse project; the choice depends on the file format and how much control you want over column types.

Function Package Reads Documented behaviour
read.csv / read.table base R CSV and delimited text Specify header explicitly; sep, na.strings, colClasses, as.is available (CRAN manual)
read_csv, read_tsv, read_delim readr CSV, TSV, general delimited Prints guessed column types; spec() returns the specification (readr site)
read_sav, read_por haven SPSS .sav and .por Value labels become labelled(); as_factor() converts; special missing values preserved (haven site)
read.spss foreign SPSS save/export files Documented in section 3.1 of the CRAN manual

More on the loaded object: data frames in R programming.

FAQ

How can I import data into R?

Use read.csv() in base R, read_csv() from readr, or read_sav() from haven for SPSS files, and assign the result to a name.

How do I import a dataset in R?

Same functions: pick the one matching the file format, then check the result with str() and head().

How to export data out of R?

Use write.csv() for CSV or write_sav() from haven for SPSS.

How to import data from SPSS to R?

Use read_sav() (haven) or read.spss() (foreign).

Sources

  • R Data Import/Export, CRAN: https://cran.r-project.org/doc/manuals/r-release/R-data.html (read 2026-11-07)
  • readr: https://readr.tidyverse.org/ (read 2026-11-07)
  • haven: https://haven.tidyverse.org/ (read 2026-11-07)
  • rlang, the .data and .env pronouns: https://rlang.r-lib.org/reference/dot-data.html (read 2026-11-07)

Follow CASRAI

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

Ask CASRAI · free to try

Ask about How to Import Data in R: read.csv, readr and haven

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 →