Handle missing values when reading CSV in R
How base R and readr turn CSV cells into NA, where the defaults differ, how to catch placeholders like n/a or -999, and how to keep missing values intact when you export.
When R reads a CSV file, it decides which cells become NA. That decision changes your data. A blank cell, the letters NA, or a placeholder such as -999 can all become missing values, and once that happens the original text is gone from the data frame. This review explains what the import step changes in base R's read.csv and in readr's read_csv, when the defaults are good enough, and when you should keep the raw text instead. No test files, package versions or measurements were supplied for this review. The behavior described here comes from the functions' documented defaults. The example outputs show expected results and were not run for this article, so confirm them against the help pages for the versions you have installed.
Short verdict: are the defaults suitable?
The defaults suit files where missing data is either an empty cell or the literal text NA, and where no real value is ever spelled NA. In that case, the main benefit is that missing entries get the right type. Numeric columns stay numeric, and functions such as mean() and sum() can skip the gaps with na.rm = TRUE. The key tradeoff is that the conversion only goes one way. After import, you cannot tell whether an NA was a blank, the word NA, or a sentinel code, and a real value that matched a missing-value marker has been lost.
Read the columns as plain text first, with no NA conversion, when any of the following applies:
• You are auditing an unfamiliar file.
• Real values could look like missing markers. Examples are the ISO country code NA for Namibia, or NA used as an abbreviation.
• Different kinds of missing data need to stay separate, such as "not asked" versus "refused".
In those cases, convert to NA column by column after you have seen the raw values.
What the import actually changes
This step changes the stored values, not just how they are displayed. When a cell's text matches one of the missing-value strings, R stores it as NA of that column's type. When you print the data, R shows NA, or <NA> in tibbles and factors. The two most common readers apply different rules by default, so switching between them can quietly change your NA counts.
Base R: read.csv
The na.strings argument defaults to "NA". Blank fields are treated as missing in logical, integer, numeric and complex columns. In character columns, a blank field stays an empty string ("") unless you add "" to na.strings.
readr: read_csv
The na argument defaults to c("", "NA"), so blank cells become NA in text columns too. readr also guesses each column's type from the first rows of the file. If a column is guessed to be numeric and a later cell holds text that isn't in na, such as "n/a", that cell becomes NA with a parsing warning. You can list these cases with problems(). This is a second way NAs can appear that has nothing to do with the na argument.
An illustrative comparison
csv_text <- "id,country,score,note
1,NA,12,
2,US,,ok
3,FR,-999,n/a"
base_df <- read.csv(text = csv_text)
# country: NA, "US", "FR" <- the Namibia code "NA" is now missing
# score: 12, NA, -999 <- blank became NA; -999 kept as a number
# note: "", "ok", "n/a" <- blank stays an empty string in a text column
readr_df <- readr::read_csv(I(csv_text)) # I() marks literal data in readr 2.x
# country: NA, "US", "FR"
# score: 12, NA, -999
# note: NA, "ok", "n/a" <- readr also treats the blank as missing here
The comments show what the documented defaults should produce. Neither reader catches -999 or n/a unless you list them. Both readers turn the country code NA into a missing value.
Matching is literal
- Spelling and case: "N/A", "n/a" and "NA" are different strings. List every variant the file uses.
- Whitespace: a cell containing " NA" with a leading space may not match. read.csv does not strip whitespace by default (strip.white = FALSE), while read_csv trims it by default (trim_ws = TRUE). Test on a sample before relying on either.
- Numeric sentinels: matching happens on the text before conversion to numbers, so "-999" does not match "-999.0" or "-999.00".
- Scope: na.strings and na apply to every column. A marker added for one column also applies to all the others.
Handling markers column by column
When a marker is only missing in some columns, read everything as text with NA conversion turned off. Then convert each column yourself. This keeps real values like the Namibia code intact.
raw <- read.csv("survey.csv", colClasses = "character", na.strings = character(0))
raw$score[raw$score %in% c("", "-999")] <- NA
raw$score <- as.numeric(raw$score)
raw$note[raw$note %in% c("", "n/a")] <- NA
# country is left untouched, so "NA" (Namibia) survives
# readr equivalent for the first step:
# raw <- readr::read_csv("survey.csv",
# col_types = readr::cols(.default = "c"),
# na = character())
Checks before trusting the import
- Look at the raw values first: read with every column as character and no NA strings, then run table() or unique() on each column to find placeholders.
- Count missing values per column with colSums(is.na(df)) and compare against what you expect from the raw file.
- With readr, run problems() on the result and check spec() to see which column types were guessed.
- Watch for numeric columns that came back as character in base R. This often means an unlisted placeholder is still in the column.
colSums(is.na(base_df))
readr::problems(readr_df)
readr::spec(readr_df)
Will the data stay usable after export and sharing?
Missing values have to be written back out as some text, so the choice matters for whoever reads the file next. Both write.csv and readr's write_csv write NA as the literal text NA by default. Other tools may read that as an ordinary string rather than a gap. If the destination expects empty cells, set na = "" when writing.
Earlier choices also carry through to the export. If you turned -999 into NA, the sentinel will not come back on export. Any downstream process that looks for -999 will no longer find it. Whether the difference between an empty string and NA survives also depends on how the writer quotes text fields. Open the exported file in a plain text editor to check what was written, rather than assuming.
No evidence was supplied about specific spreadsheets, databases or other destinations. A file that imports correctly into one tool may not import the same way into another. Test with the actual destination before sharing widely.
Gaps in this review
This review is based on documented default behavior, not on supplied test files or measured results. It does not cover data.table's fread, vroom or other readers in detail. Their handling of empty fields, especially in character columns, should be checked in their own help pages. Defaults can change between package releases, so before you rely on them, confirm them with ?read.table and ?readr::read_csv for your installed versions.