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"), orinstall.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
- Call
read.table()(or its CSV wrapperread.csv()) with the file path and assign the result:df <- read.csv("mydata.csv"). - Set
headerexplicitly. The manual says: “We recommend that you specify the header argument explicitly.” - For tab-separated text, set the separator:
read.table("mydata.txt", header = TRUE, sep = " "). - If the file marks missing values with a string such as “NA” or “-“, list it in
na.strings. - To fix column types, pass
colClasses; the manual notes this is faster and uses less memory than letting R guess. Useas.is = TRUEto stop character columns being converted to factors.
Import with readr
- Install and load readr:
install.packages("readr"), thenlibrary(readr). - Read the file:
df <- read_csv("mydata.csv"). readr’s own example isread_csv(readr_example("chickens.csv")). - 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.
install.packages("haven"), thenlibrary(haven).df <- read_sav("file.sav").- Haven turns SPSS value labels into the
labelled()class and preserves special missing values. Convert a labelled column to an ordinary factor withas_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:
- Look at the first rows with
head(df)and the structure withstr(df): are the column names right and is each column the type you expect (numeric, character, factor)? - With readr, read the printed column specification, or call
spec(df). - If numbers arrive as text, look for a stray non-numeric string and add it to
na.strings, or declare the type withcolClasses. - If the first data row became column names (or the reverse), set
headerexplicitly.
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
sepin base R, or useread_csv2for semicolon-separated files with comma decimals, orread_delimfor another delimiter. - Text appears as factors: pass
as.is = TRUEin base R. - Wrong column types: set
colClasses(base R) or copy the output ofspec()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)







