Read selected CSV columns in R
How to read only certain columns from a CSV in R with read.csv, readr and data.table, what column selection changes, and how to keep values like ZIP codes intact.
Is reading only certain columns the right choice?
Reading a subset of columns works well when you know which fields you need and the file is wide or large. The main benefit is a smaller, more focused data frame that uses less memory and is easier to work with. The main tradeoff is that the decision is made at import time. Any column you skip never reaches R, so needing it later means reading the file again. Also, the columns you keep are still type-guessed unless you declare their types. Reading the whole file is the better default when the file is small, when you don't yet know its structure, or when you need other columns to check or join the data.
What column selection actually changes
Selecting columns doesn't modify the CSV file. It only controls which fields end up in the R object. It also isn't a character-level transformation: values in the kept columns are parsed the same way they would be in a full read. The reader generally still has to scan every line to find field boundaries. Skipping columns saves the memory and conversion work for the dropped fields, but it doesn't let the parser skip the file entirely.
The parts that can differ between approaches are the column names, column order and column types in the result. These matter more than the selection itself for whether the data still means what it meant in the source file.
Sample file used in the examples
id,name,zip,order date,amount
1,Ana,02134,2026-01-05,19.99
2,Lee,10001,2026-01-06,5.00
This sample is illustrative, not real data. Watch the zip column. If a reader parses it as a number, 02134 becomes 2134 and the value no longer matches a real ZIP code. The order date header also contains a space, which some readers rename.
Base R: read.csv with colClasses
Base R has no argument that takes column names to keep. Instead, you give colClasses one entry per column and set "NULL" for each column you want to skip. NA tells R to guess the type for that column.
# Positional: one entry per column, in file order
orders <- read.csv("orders.csv",
colClasses = c("integer", "NULL", "character", "NULL", "NULL"))
# By name: read the header first, then build colClasses
hdr <- names(read.csv("orders.csv", nrows = 1, check.names = FALSE))
keep <- c("id", "zip")
classes <- ifelse(hdr %in% keep, NA, "NULL")
classes[hdr == "zip"] <- "character"
orders <- read.csv("orders.csv", colClasses = classes)
Kept columns stay in file order. The positional form breaks silently if a column is added or moved in the source file, so building colClasses from the header is safer for files you don't control. Without the explicit "character" entry, read.csv would read zip as an integer and drop the leading zero.
readr: col_select
In readr, read_csv takes a col_select argument that accepts bare names, positions or tidyselect helpers such as starts_with(). Names containing spaces need backticks. You can declare types for kept columns through col_types. Alternatively, cols_only() can do both jobs: it keeps only the columns it lists and assigns their types.
library(readr)
orders <- read_csv(
"orders.csv",
col_select = c(id, zip, `order date`),
col_types = cols(zip = col_character())
)
col_select is available in readr 2.0 and later. Older versions only offer the cols_only() route. The result is a tibble, and header names are kept as written, so order date stays order date. Automatic guessing may treat zip as a number, so declaring it as character removes the dependence on the guess.
data.table: fread with select or drop
library(data.table)
orders <- fread("orders.csv",
select = c("id", "zip"),
colClasses = list(character = "zip"))
# Or name the columns to leave out
orders <- fread("orders.csv", drop = c("name", "amount"))
select and drop accept column names or numbers, and select can't be combined with drop in the same call. fread drops leading zeros by default when it reads a column as numeric. Either declare the column as character, as above, or set keepLeadingZeros = TRUE. The result is a data.table; wrap it in as.data.frame() if downstream code expects a plain data frame. Check the column order of the result rather than assuming it matches either the file or your select vector.
Choosing among the three
- read.csv: no extra packages, but selection is indirect through colClasses and names are altered by default.
- readr read_csv: selection by name or helper, names preserved, returns a tibble.
- data.table fread: concise select and drop arguments, returns a data.table, useful if you already use data.table.
Will the result stay usable after export or sharing?
Types are the most likely way to lose meaning. Identifiers with leading zeros, such as ZIP codes, account numbers and product codes, should be read as character. Base read.csv also leaves dates as character unless you convert them. Code that later treats them as dates needs that conversion step.
Names can also cause problems. read.csv converts headers to syntactic names by default, so order date becomes order.date. A script or colleague expecting the original header won't find it. Set check.names = FALSE to keep the original header, or rename the columns deliberately.
Writing a subset back out can add or change things. write.csv adds a row-name column unless you pass row.names = FALSE, and it uses the session's native encoding unless fileEncoding is set. readr's write_csv writes no row names and uses UTF-8. Both quote fields that contain the delimiter. The output is ordinary delimited plain text with no styled or substituted characters, so it copies and pastes like any other CSV. How it displays in a particular spreadsheet or assistive tool was not tested for this review.
Gaps in this review
No benchmark data was supplied, so this review makes no claims about speed or memory savings. Those depend on file size, file width and package version. To measure them, time both a full read and a selective read of your own file. Argument names and defaults reflect documented behavior of current package releases. Confirm them in the help pages for your installed versions (?read.csv, ?readr::read_csv, ?data.table::fread) before relying on them in shared code.