Parsing TSV and Fixed-Width Files
Use read_tsv() and read_fwf() for tab-separated and fixed-width data.
Beyond CSV: Other File Formats
Not all tabular data comes as CSV. Tab-separated files (TSV), pipe-delimited files, and fixed-width format (FWF) files are common in government data, legacy systems, and scientific databases. The readr package handles all of these with consistent syntax.
library(readr)
# readr's flat file readers:
# read_csv() - comma-separated
# read_csv2() - semicolon-separated (European)
# read_tsv() - tab-separated
# read_delim() - any delimiter
# read_fwf() - fixed-width format
# read_lines() - raw text, one line per element
cat('All readr functions return tibbles with the same interface!')read_tsv() — Tab-Separated Values
read_tsv() reads tab-delimited files. It has the same arguments as read_csv() — col_types, na, skip, etc. TSV is preferred over CSV when data contains commas (addresses, descriptions).
library(readr)
# Simulated TSV using \t as delimiter
tsv_data <- 'name\tscore\tcity
Alice Smith\t85\tNew York, NY
Bob Jones\t92\tLos Angeles, CA
Carol White\t78\tChicago, IL'
# Note: cities contain commas — TSV handles this cleanly
df <- read_tsv(tsv_data, show_col_types=FALSE)
print(df)All lessons in this course
- Reading CSV Files with read_csv()
- Parsing TSV and Fixed-Width Files
- Importing Excel Files with readxl
- Writing Data to Multiple Formats