R missing values
last modified November 19, 2026
Real data is rarely complete. A sensor fails, a respondent skips a question,
or a value simply does not exist for a record. R represents an absent value
with NA, and it treats that value differently from every ordinary
number: most operations propagate it instead of guessing. This article
explains the difference between NA, NaN, and
NULL, shows how to detect and remove missing values, how to make
summaries ignore them, and how a data frame reports which of its rows are
complete.
Missing values propagate
NA means not available
. It is not zero and not an empty
string, and any arithmetic that involves it returns NA, because
the result cannot be known. The function is.na tests for it and
returns a logical vector.
scores <- c(72, NA, 90, 66) print(scores) print(is.na(scores)) print(sum(scores)) print(mean(scores))
The second score is missing, so the fourth line marks it and the last two
lines return NA. This propagation is deliberate: a sum that
silently ignored a gap would be wrong without warning. The next sections show
how to ask for the gap to be ignored explicitly.
$ Rscript na.R [1] 72 NA 90 66 [1] FALSE TRUE FALSE FALSE [1] NA [1] NA
Typed missing values
A bare NA is logical, but every atomic type has its own missing
value. NA_integer_, NA_real_, and
NA_character_ are the typed forms. NaN, which means
not a number
, is a numeric value produced by an undefined operation,
and it also counts as missing.
print(typeof(NA)) print(typeof(NA_integer_)) print(typeof(NA_real_)) print(typeof(NA_character_)) print(is.nan(NaN)) print(is.na(NaN))
The four missing values have four different types, which is why a vector can
hold an NA without changing type. is.nan(NaN) is
true and is.na(NaN) is also true, because every NaN
is missing. The reverse is not true: is.nan(NA_real_) is false,
because an unavailable number is not the same as an invalid one.
$ Rscript typed.R [1] "logical" [1] "integer" [1] "double" [1] "character" [1] TRUE [1] TRUE
Summaries that ignore gaps
The argument na.rm = TRUE removes missing values before the
summary is computed. It is available in sum, mean,
median, sd, min, max, and
many other functions. Without it, one missing value makes the whole result
missing.
scores <- c(72, NA, 90, 66) print(mean(scores)) print(mean(scores, na.rm = TRUE)) print(sum(scores, na.rm = TRUE)) print(median(scores, na.rm = TRUE))
The first mean is NA; the second averages the three known scores
and returns 76. The sum of the known values is 228, and the median of 66, 72,
and 90 is 72. Removing the missing value changes the denominator, which is
the correct behaviour when the gap means that no observation exists.
$ Rscript functions.R [1] NA [1] 76 [1] 228 [1] 72
Detecting missing values
is.na marks every missing value, including NaN.
anyNA answers whether a vector contains at least one, and
which returns the positions. Negating the logical vector keeps
only the observed values.
scores <- c(72, NA, NaN, 90) print(is.na(scores)) print(anyNA(scores)) print(which(is.na(scores))) print(scores[!is.na(scores)])
Positions two and three are missing: one NA and one
NaN. The filtered vector contains 72 and 90. Testing for
missingness with == NA would not work, because the comparison
itself returns NA; is.na is the only reliable test.
$ Rscript detecting.R [1] FALSE TRUE TRUE FALSE [1] TRUE [1] 2 3 [1] 72 90
Missing values in a data frame
is.na on a data frame returns a logical matrix with one row and
one column per cell. complete.cases reduces that matrix to one
logical value per row, which is exactly what is needed to drop incomplete
records. Both functions treat every column, whatever its type.
students <- data.frame(
name = c("Ada", "Grace", "Alan", "Edsger"),
score = c(90, NA, 66, 78)
)
print(students)
print(is.na(students))
print(complete.cases(students))
print(students[complete.cases(students), ])
Only the second row is incomplete, because its score is missing. The
complete.cases vector is true for the other three rows, and
indexing with it returns a table of complete records. The row names stay
attached, so the original position of each record remains visible.
$ Rscript dataframes.R
name score
1 Ada 90
2 Grace NA
3 Alan 66
4 Edsger 78
name score
[1,] FALSE FALSE
[2,] FALSE TRUE
[3,] FALSE FALSE
[4,] FALSE FALSE
[1] TRUE FALSE TRUE TRUE
name score
1 Ada 90
3 Alan 66
4 Edsger 78
Omitting missing values
na.omit removes every element or row that contains a missing
value. On a vector it returns a shorter vector with an na.action
attribute that records the dropped positions. On a data frame it removes
whole rows and keeps the attribute for the same purpose.
scores <- c(72, NA, 90, 66, NaN)
clean <- na.omit(scores)
print(clean)
cat("length:", length(clean), "\n")
cat("dropped positions:", as.integer(attr(clean, "na.action")), "\n")
The cleaned vector contains 72, 90, and 66. The attribute records positions
two and five, which held NA and NaN. The attribute
is useful for an audit trail, but it can surprise a function that expects a
plain vector, so strip it with as.numeric when only the values
matter.
$ Rscript omitting.R [1] 72 90 66 attr(,"na.action") [1] 2 5 attr(,"class") [1] "omit" length: 3 dropped positions: 2 5
Filling missing values
Sometimes a gap should be replaced rather than removed. A common choice is
the mean of the observed values, which keeps the sample size and the overall
average but reduces the variance. The assignment selects the missing
positions with is.na and writes the replacement into them.
scores <- c(72, NA, 90, 66, NA) scores[is.na(scores)] <- mean(scores, na.rm = TRUE) print(round(scores, 2))
The two gaps are replaced by 76, the mean of 72, 90, and 66. The rounded output shows that the stored values keep full precision. Mean imputation is simple and transparent, but it is not suitable when the missingness carries information; the descriptive statistics article later in the series discusses the alternatives.
$ Rscript filling.R [1] 72 76 90 66 76
A complete example
The next script separates usable records from incomplete ones and reports how many were dropped. It then computes the mean score from all available values, which is different from the mean of the complete rows. Keeping the two questions separate avoids silently changing the population being summarised.
students <- data.frame(
name = c("Ada", "Grace", "Alan", "Edsger"),
score = c(90, NA, 66, 78),
subject = c("math", "art", "math", NA)
)
usable <- complete.cases(students)
print(students[usable, ])
cat("dropped:", sum(!usable), "rows\n")
cat("mean score:", mean(students$score, na.rm = TRUE), "\n")
Two rows are dropped: Grace has no score and Edsger has no subject. The mean
over the remaining scores is 78, computed from 90, 66, and 78. If the
incomplete row had been included, the mean would have been NA
unless na.rm were set, which is why both the filter and the
summary state their treatment of missing data explicitly.
$ Rscript report.R name score subject 1 Ada 90 math 3 Alan 66 math dropped: 2 rows mean score: 78
Next steps
The subsetting article explains the logical
indices used here in more detail, and the
apply family article applies summaries to
groups of rows while passing na.rm through to the function. The
descriptive statistics article later in the series compares deletion and
imputation as strategies.
Source
An Introduction to R: missing values
R documentation: na.omit and na.exclude
R documentation: complete.cases
In this article we have seen why NA propagates through
arithmetic, and how the typed missing values and NaN differ. We
used na.rm to summarise the observed values, detected gaps with
is.na and anyNA, and removed incomplete rows with
complete.cases and na.omit. We filled gaps with the
mean and audited a table before and after dropping records. The series
continues with the R apply family.
Author
List all R Language tutorials.