Read TSV Files in R: Base R, readr and data.table Compared

How to read tab-separated files in R with read.delim, readr::read_tsv and data.table::fread, what each parser changes in your data, and how to keep results usable.

A tab-separated values (TSV) file is plain text: one record per line, with fields separated by tab characters. Reading it into R turns that text into a data frame, and the conversion involves choices about column types, missing values, quoting, encoding and column names. This review covers the three common ways to load a TSV file in R, what each one changes along the way, and how to tell whether the result still matches the source data.

Which approach suits your task

For most work, use readr::read_tsv. It returns a tibble, leaves column names as written, reports the column types it chose, and lets you set types explicitly. Its main cost is a package dependency. Base R's read.delim needs no packages and suits scripts that must run on a minimal installation, but it renames columns and its type handling is less transparent. data.table::fread is the usual choice for very large files where speed matters, and it works well if the rest of your code already uses data.table.

All three methods share the same tradeoff: they parse text into typed columns, so values can change meaning if a type guess is wrong. When exact source values matter more than convenience, read every column as character. Examples include ZIP codes, account IDs, gene names, or files you will pass on unchanged. Convert only the columns you need. Plain character input is also the safer choice when you only need to inspect, filter or re-export a file.

Basic syntax for each method

# Base R (no packages)
df <- read.delim("data.tsv")

# Equivalent, with the separator spelled out
df <- read.table("data.tsv", sep = "\t", header = TRUE)

# readr
library(readr)
df <- read_tsv("data.tsv")

# data.table
library(data.table)
dt <- fread("data.tsv", sep = "\t")

The two base R calls differ in their defaults. read.delim assumes a header row, treats only double quotes as quote characters and does not treat any character as a comment marker. read.table defaults to no header, treats both single and double quotes as quotes, and treats # as the start of a comment. If you use read.table on a TSV file, an apostrophe in a text field or a # inside a value can silently corrupt the import. Unless you have a reason to override these defaults, read.delim is the safer base R option.

What reading a TSV file changes

The file on disk is never modified. The changes happen in the in-memory copy, and some of them affect meaning.

  • Column types: each parser guesses whether a column is numeric, logical, date or character. A column such as 00501 can become the number 501, losing its leading zeros. Long numeric IDs can lose precision once stored as doubles.
  • Missing values: base R treats the string NA as missing by default. readr treats both empty fields and NA as missing. A literal value such as "NA" (for example, the code for Namibia) can therefore become missing.
  • Column names: read.delim applies check.names = TRUE by default. Names containing spaces or symbols become syntactic names, so "order id" becomes order.id, and duplicate names are made unique. readr and fread keep the original names.
  • Quoting: quote characters around fields are removed, and quoted content is read as a single field. Strict TSV usually has no quoting at all, so a stray quote mark inside a field can merge rows. Setting quote = "" in base R, or the equivalent option in other readers, turns quote handling off.
  • Whitespace and comments: depending on the reader and its settings, leading or trailing spaces may be trimmed and lines starting with a comment character may be dropped.
  • Strings as factors: since R 4.0, base readers keep text as character by default. Older code or older R versions may convert text columns to factors.

To stop type guessing from altering values, declare the types yourself:

# readr: everything as character, then convert selectively
df <- read_tsv("data.tsv", col_types = cols(.default = col_character()))

# readr: specific columns
df <- read_tsv("data.tsv", col_types = cols(zip = col_character(), amount = col_double()))

# Base R
df <- read.delim("data.tsv", colClasses = "character", check.names = FALSE)

# data.table
dt <- fread("data.tsv", colClasses = "character")

readr guesses types from a sample of rows. If a column looks numeric early on but contains text further down, the guess can be wrong. In that case, readr records parsing problems instead of stopping. Call problems(df) after reading to see them, or raise the guess_max argument.

Encoding and special characters

Text in a TSV file is only read correctly if R knows the file's encoding. UTF-8 is the common default. Files exported from older spreadsheet software may use a legacy encoding instead, which shows up as garbled accented letters or symbols. Declare the encoding explicitly when it is known:

df <- read.delim("data.tsv", fileEncoding = "latin1")

df <- read_tsv("data.tsv", locale = locale(encoding = "latin1"))

dt <- fread("data.tsv", encoding = "Latin-1")

Neither R nor these packages can reliably detect an encoding on their own. readr provides guess_encoding() as a heuristic aid, but its result is a probability estimate, not a confirmation. Check a few rows that contain non-ASCII text before relying on the import. A byte order mark at the start of a UTF-8 file can also end up attached to the first column name. If that name looks odd, compare it against the header in a text editor.

Will the data stay usable after export or sharing

Reading and writing are separate steps, and a round trip is not guaranteed to reproduce the original file. Type conversion, renamed columns, reformatted numbers and dates, and changed missing-value markers can all change what gets written back. If someone downstream needs the file byte-for-byte or value-for-value, read with character columns and original names, then write without added quotes or row names:

# Base R
write.table(df, "out.tsv", sep = "\t", quote = FALSE,
            row.names = FALSE, na = "")

# readr
write_tsv(df, "out.tsv", na = "")

# data.table
fwrite(dt, "out.tsv", sep = "\t")

Tab characters and line breaks inside field values are the main compatibility risk. The TSV convention has no standard escape for them, so a field containing a tab or newline will break the column structure for many readers, even if it was written with quotes. If your data can contain these characters, replace them before export, or use CSV with proper quoting instead.

Spreadsheet applications that open a TSV file apply their own type guessing. Long numbers, leading zeros and date-like strings may be altered on import there, regardless of how carefully R wrote the file. This depends on the application and its settings. The sources for this article include no tested results for any specific program, so check in the destination application before relying on it.

A quick verification checklist

  • Compare nrow() against the number of data lines in the file to catch merged or dropped rows caused by quotes or comment characters.
  • Check names() for renamed or duplicated columns.
  • Run str() or glimpse() to confirm each column's type is what you intended.
  • In readr, inspect problems() for parsing failures.
  • Spot-check rows containing leading zeros, long IDs, the literal text NA, quote marks and non-ASCII characters.

Gaps in this review

This review is based on the documented behavior of base R, readr and data.table, not on measurements. It includes no speed or memory benchmarks, no version-by-version comparison of defaults beyond those noted, and no tests against particular spreadsheet or database tools. Defaults can change between package releases, so confirm them in the help pages for the versions you have installed, using ?read.delim, ?read_tsv and ?fread.