readr read_table() in R: Read Whitespace-Separated Files
The readr read_table() function reads a whitespace-separated text file into a tibble. It treats any run of spaces or tabs as a single separator, so neatly aligned columns and ragged spacing both parse with one call.
read_table("data.txt") # whitespace-separated file
read_table(I("x y\n1 2")) # read whitespace text directly
read_table("data.txt", col_names = FALSE) # file has no header row
read_table("data.txt", col_names = c("a", "b")) # supply column names
read_table("data.txt", skip = 2) # skip junk lines before header
read_table("data.txt", na = c("NA", ".")) # set the NA strings
read_table("data.txt", n_max = 100) # read only the first 100 rowsNeed explanation? Read on for examples and pitfalls.
What read_table() does
read_table() reads a file whose columns are separated by whitespace. You give it a file path, a URL, or literal text. The function splits each line on every run of spaces or tabs, guesses each column type from the first rows, and returns a tidy tibble.
The defining feature is how it handles the separator. A comma file has exactly one comma between fields, but a whitespace file often uses padding to line columns up visually. read_table() collapses one space and ten spaces to the same thing, so console dumps, .dat files, and space-aligned scientific output all read cleanly.
Syntax and key arguments
Most reads need only file; the rest of the arguments tune parsing. The signature mirrors the other readr readers, which makes switching between them easy.
There is no delim argument because the delimiter is fixed: whitespace. If your file uses a single specific character such as a pipe or semicolon, reach for read_delim() instead and pass that character.
read_table("data.txt") is pandas.read_csv("data.txt", sep=r"\s+"). The pandas regex separator \s+ matches the same runs of whitespace that readr's read_table() collapses automatically.read_table() examples
Start with the simplest case: clean, single-spaced columns. Wrapping literal text in I() lets every example run without a file on disk.
The first line became the header and both data rows parsed as dbl (double). No delimiter argument was needed.
Ragged spacing is where read_table() earns its place. Columns padded to different widths still parse, because every run of spaces counts as one separator.
The uneven gaps between name, age, and city made no difference. read_delim() would have produced empty columns from those extra spaces.
Read a real file from disk. Write a numeric data frame with base write.table(), turning off row names and quotes so the output is plain whitespace.
Supply your own column names when the file has no header. Pass a character vector to col_names, and read_table() treats the first line as data.
Declare your own missing-value markers. Real exports use codes like . or n/a. List them in na so they parse as NA instead of forcing the column to text.
col_types skips the guessing scan, silences the column-spec message, and locks types across files. Use cols(id = col_integer(), .default = col_double()) to fix one column and default the rest.read_table() vs read_delim() and read_fwf()
The three readers differ only in how they find column boundaries. Picking the right one is a question about your file's separator.
| Function | Separator | When to use |
|---|---|---|
read_table() |
any run of whitespace | space-aligned columns, ragged padding |
read_delim() |
one fixed character | pipe, semicolon, or custom single-char files |
read_csv() |
comma, fixed | standard comma-separated files |
read_tsv() |
tab, fixed | tab-separated files |
read_fwf() |
fixed column positions | no separator; fields defined by character width |
Use read_table() when fields are separated by gaps of varying width. Use read_delim() when exactly one character sits between fields. Use read_fwf() when there is no separator at all and columns are defined purely by position.
Common pitfalls
Text fields that contain spaces break the column count. Because every run of whitespace is a separator, a value like New York splits into two fields and shifts the rest of the row.
read_fwf() with explicit column positions.Passing literal text without I(). A bare string is treated as a file path. Wrap inline data in I() so read_table() parses it as content.
Trusting guessed types on messy columns. read_table() infers each type from the leading rows. A numeric column with stray text far down reads as character. Set col_types explicitly when a column matters.
Try it yourself
Try it: Write the data frame below to a whitespace file, read it back with read_table(), then sum the sales column. Save the total to ex_total.
Click to reveal solution
Explanation: write.table() with row.names = FALSE and quote = FALSE produces plain whitespace-separated text. read_table() reads it back into a tibble, and sum() over the sales column adds the four weekly figures.
Related readr functions
read_table() is one reader in the readr import family. Reach for the sibling that matches your file's separator.
read_delim(): read files with one fixed single-character delimiter.read_csv(): read comma-separated files.read_tsv(): read tab-separated files.read_fwf(): read fixed-width files where columns are defined by position.read_lines(): read a file as a raw character vector when no parsing is wanted.
For the full argument reference, see the readr read_table documentation on tidyverse.org.
FAQ
What is the difference between read_table() and read_delim() in R?
read_table() separates columns on any run of whitespace, so single spaces and aligned padding both work. read_delim() separates on exactly one character that you set with the delim argument. Use read_table() for space-aligned text and console output. Use read_delim() when a single specific character such as a pipe or semicolon sits between fields.
How do I read a space-separated file in R?
Call read_table("data.txt"). The function splits each line on whitespace, guesses column types from the first rows, and returns a tibble. If the file has no header, add col_names = FALSE or pass a character vector of names. To read inline text instead of a file, wrap the string in I().
What happened to read_table2() in readr?
read_table2() was the lenient whitespace reader in older readr versions. Since readr 2.0, its behavior was merged into read_table(), and read_table2() is deprecated. Modern code should call read_table() directly. If you see read_table2() in an old script, replace it with read_table() and the result is the same.
Why does read_table() split my text column into two?
read_table() treats every run of whitespace as a separator, so a value containing an internal space, such as New York, becomes two fields. That shifts every column after it. If your data has multi-word text fields, the file is really fixed-width. Use read_fwf() with explicit column positions so the spaces inside fields are preserved.
Can read_table() read a file from a URL?
Yes. Pass an http:// or https:// URL as the file argument and read_table() downloads and parses it in one step. For a file you will reuse, download it once with download.file() and read the local copy to avoid repeated network requests.