Convert Text File to CSV in R
Convert text files to CSV in R three ways: base R, the readr package, and data.table for large files. Copy-paste examples for tab, pipe and custom delimiters, with notes on encoding and types.
Convert text to CSV in R
R reads delimited text into a data frame and writes CSV with one function, which makes converting text files quick whether you use base R or a package. For big files, data.table is dramatically faster. All three approaches are shown below.
For a single ad-hoc file, the browser converter is the fastest path; use R when the conversion is part of an analysis or script.
Base R: read.delim and write.csv
No packages required. read.delim assumes tab-delimited with a header; read.table lets you set any separator:
# tab-delimited input
df <- read.delim("input.txt", stringsAsFactors = FALSE)
write.csv(df, "output.csv", row.names = FALSE)
# any delimiter, e.g. pipe
df <- read.table("input.txt", sep = "|", header = TRUE,
stringsAsFactors = FALSE)
write.csv(df, "output.csv", row.names = FALSE)readr: read_delim and write_csv
The readr package is faster, infers column types, and defaults to UTF-8:
library(readr)
df <- read_delim("input.txt", delim = "\t") # use "|" or "," as needed
write_csv(df, "output.csv")write_csv never writes row names and quotes fields only when necessary, producing clean output for Excel and other tools.
data.table: fread and fwrite (fast)
For large files, data.table is the fastest option and auto-detects the delimiter:
library(data.table)
dt <- fread("input.txt") # auto-detects the separator
fwrite(dt, "output.csv") # fast, correctly quoted CSVIf a column of IDs loses leading zeros, read it as character with colClasses = "character" (base) or col_types (readr).
Types, encoding and large files in R
R’s defaults are sensible but worth tuning. Base read.csv and read.delim historically converted strings to factors; modern R (4.0+) does not, but older scripts often pass stringsAsFactors = FALSE for safety — harmless to keep. Identifier columns that must retain leading zeros should be read as character via colClasses (base) or col_types = cols(.default = "c") (readr), since numeric coercion drops them.
Encoding is the other common snag: if non-ASCII text appears mangled, set fileEncoding = "UTF-8" in base R, or rely on readr and data.table, which assume UTF-8. For genuinely large files, data.table::fread is not just faster — it is more memory-efficient and detects the delimiter and column types automatically, which makes it the pragmatic default for anything above a few hundred megabytes.
Verify a conversion with str(df) or head(df) to confirm column types and that the header was parsed, then write with row.names = FALSE so R does not add an extra index column.
One more practical note: R writes NA for missing values by default, which may not be what a downstream system expects. Pass na = "" to write.csv or write_csv to emit empty cells instead. If your columns are in a different order than the target requires, reorder them with df[, c("col1", "col2")] before writing, since CSV preserves whatever order the data frame has. These small adjustments mean the file other tools receive matches their schema exactly, with no manual cleanup afterwards.
In short, R turns a text file into a CSV in a single line; the only real decisions are which reader to use — base for simplicity, readr for clean defaults, data.table for speed — and whether any column needs to stay as character. Once those are set, the same script converts every similar file the same way. That repeatability is what makes R a natural fit for data pipelines and reproducible, shareable analysis.
Convert Text File to CSV in R — FAQ
How do I convert a text file to CSV in R?
Read it with read.delim/read_delim/fread, then write with write.csv/write_csv/fwrite.
Which is fastest for large files?
data.table::fread and fwrite are the fastest by a wide margin.
How do I keep leading zeros?
Read the column as character: colClasses="character" in base R or col_types in readr.
How do I set the encoding?
readr and data.table use UTF-8 by default; in base R pass fileEncoding="UTF-8".