NA is contagious, and it is the best decision R ever made

Here is a line of R that catches every newcomer, usually in their first hour.

revenue <- c(1200, 980, NA, 1430, 1100)
sum(revenue)
#> [1] NA

The obvious reaction is that R is being difficult. Four of those five numbers are perfectly good. A spreadsheet would have added them and returned 4,710. Python's sum() would have raised an error on the None, and pandas would have quietly returned 4,710 as well. Every tool you have used before either refuses or gets on with it.

R returns NA, and it is right, and the reason is worth understanding because it changes how you think about every dataset you will ever touch.

What NA actually means

NA is not zero. It is not an empty string, and it is not a null pointer. It means this value exists and I do not know what it is.

That distinction is the whole thing. A sale of zero is a fact. A sale you failed to record is not a fact about the sale — it is a fact about your data collection. Conflating the two is how a wrong number gets published.

So: what is the sum of 1200, 980, 1430, 1100 and a number I do not know?

It is not 4,710. It is 4,710 plus something. The honest answer is "I cannot tell you", and NA is R saying exactly that. Every other tool in that list answered a question you did not ask — "what is the sum of the values you happen to have?" — and presented it as the answer to the one you did.

R's NA propagates for the same reason that NaN propagates through floating point, and the same reason a bank will not tell you your balance when a transaction is still pending.

The part that looks like pedantry and is not

Try the comparison that everyone tries next:

x <- NA
x == NA
#> [1] NA

This looks absurd. Surely NA equals NA?

No — and once you see why, the design clicks. You have two values, and you know nothing about either. Are they the same? You cannot know. One might be 5 and the other 12. NA == NA asking "are these two unknowns equal" can only honestly answer "unknown".

Which is why R gives you a separate function for the question you actually meant:

is.na(x)
#> [1] TRUE

is.na() asks "is this value missing", which is a question about the container, not about the unknown value inside it. Those are genuinely different questions and R makes you pick one. The first time you write filter(df, x != NA) and get zero rows back, you will understand why is.na() exists.

The same logic runs through the logical operators, and here R is more careful than most people expect:

NA & FALSE
#> [1] FALSE
NA | TRUE
#> [1] TRUE
NA & TRUE
#> [1] NA

NA & FALSE is FALSE because whatever that unknown is, something AND FALSE is false. NA | TRUE is TRUE for the mirror reason. But NA & TRUE depends entirely on the unknown, so it stays unknown. This is three-valued logic, the same system SQL uses, and it is doing real reasoning rather than throwing up its hands.

Saying what you want, in writing

R does not stop you ignoring missing values. It stops you doing so accidentally:

sum(revenue, na.rm = TRUE)
#> [1] 4710
mean(revenue, na.rm = TRUE)
#> [1] 1177.5

na.rm = TRUE is six keystrokes, and typing them is a decision that appears in your code where a reviewer can see it. Compare that with a spreadsheet, where the same decision is made by the software, invisibly, on your behalf, and leaves no trace anywhere in the file.

Notice what mean() did there. It returned 1,177.5 — the mean of the four values it could see. Not 942, which is what you get if you treat the missing value as zero. Both are defensible under some assumption, and they are 25% apart. The software cannot know which you meant, and R's answer is to make you say.

Where it stops being philosophical

A concrete case. You have survey responses with an income field, and some are blank. You want the mean income.

mean(survey$income, na.rm = TRUE)

Fine — except the question nobody asked is why those are blank. If people declined to answer because their income was high, then the respondents you dropped are systematically different from the ones you kept, and your mean is not an estimate of the population mean. It is an estimate of the mean among people willing to disclose, which is a different and lower number.

This is the distinction statisticians call missing completely at random against missing not at random, and it is the difference between a defensible analysis and a misleading one. The point is not that R solves it — nothing solves it automatically. The point is that R's refusal to quietly skip the gaps is what makes you look at them. A tool that silently returns 4,710 never prompts the question at all.

The same applies to the fix people reach for first. Replacing missing values with the column mean is the most common imputation and one of the worst: it shrinks the variance, strengthens correlations that were not there, and makes your confidence intervals too narrow — all while making the dataset look complete. If you impute, record that you did:

survey <- survey |>
  mutate(
    income_imputed = is.na(income),
    income = ifelse(is.na(income), median(income, na.rm = TRUE), income)
  )

That flag column costs nothing and means every downstream analysis can ask how many rows were invented, and check whether the result depends on them.

Four kinds of absence, and they are not interchangeable

R has four values that all look like "nothing" and mean four different things. Conflating them is its own small category of bug.

NA     # a value exists; I do not know what it is
NULL   # there is no value here at all
NaN    # the result of an undefined arithmetic operation
Inf    # a number too large to represent, or a division by zero

The practical difference shows up immediately in a vector:

c(1, NA, 3)      # length 3 - the gap occupies a position
c(1, NULL, 3)    # length 2 - the NULL simply is not there

NA is a placeholder in a sequence; NULL is the absence of an element. That is why removing a list element is l$x <- NULL and why setting it to NA does something entirely different — the first deletes the slot, the second keeps the slot and marks its contents unknown.

NaN arises from arithmetic that has no answer, and Inf from arithmetic whose answer is unbounded:

0 / 0       #> NaN
1 / 0       #> Inf
log(-1)     #> NaN, with a warning
mean(c())   #> NaN

The trap: is.na(NaN) returns TRUE, because a NaN is indeed not available — but is.nan(NA) returns FALSE. The relationship runs one way only. If you want to know whether a gap came from missing data or from a failed calculation, is.nan() is the one that distinguishes them, and that distinction matters because they have different causes. Missing data is a collection problem; NaN is usually a division by a group that turned out to be empty.

And Inf is a number, which means it passes straight through the checks you set up for missing data:

x <- c(1, 2, Inf)
sum(is.na(x))       #> 0     - nothing missing here
mean(x)             #> Inf   - and yet

A column with an Inf in it will sail past drop_na() and poison every summary downstream. is.finite() is the check that catches all of them at once, and it is worth preferring over is.na() whenever the column came from a division.

Where NA changes the shape of your results

Two places it does something structural rather than arithmetic, both worth knowing before they surprise you.

In grouping, NA is a group. count(df, region) on data where some regions are missing gives you a row labelled NA alongside the real ones — which is correct and is frequently the most interesting row in the output, because it tells you how much of your data could not be attributed to anywhere. group_by() has a .drop argument if you want it gone, but look at it first.

In sorting, missing values go last regardless of direction. arrange(df, revenue) and arrange(df, desc(revenue)) both leave the NA rows at the bottom. This is deliberate and it is the opposite of what a naive comparison would do — but it means "the top ten by revenue" and "the bottom ten by revenue" are both computed from the rows that have a revenue, which is almost certainly what you wanted and is worth knowing you got.

Finding them before they find you

Three lines that should start every analysis:

colSums(is.na(df))                 # how many, per column
mean(complete.cases(df))           # what proportion of rows are usable
df |> summarise(across(everything(), \(x) mean(is.na(x))))

The second one is the one people skip and regret. If 8% of each of six columns is missing, and the gaps are in different rows, you may have lost 40% of your data to drop_na() without noticing. The row count after cleaning is a number worth printing and worth reporting.

Then look at where they are:

library(naniar)
vis_miss(df)      # a map of the whole dataset, missing cells shaded
gg_miss_upset(df) # which columns go missing together

Missingness is almost never uniform. It clusters — by date when a sensor failed, by region when a form changed, by respondent type when a question was sensitive. The pattern tells you what happened, and what happened determines what you are allowed to do about it.

The cost, honestly

This design is not free. NA propagates into places you did not expect, and some of them are genuinely annoying. A single missing value in a column turns a correlation matrix entirely NA unless you pass use = "complete.obs". Joins produce NA for non-matching rows, which is correct and still surprises people. And there are several NAs — NA_integer_, NA_character_, NA_real_ — because R's vectors are typed and the missing value has to be typed too. You will meet that when a function complains about a type you thought you had.

There is also the opposite failure, where a dataset codes missing values as -999, "N/A" or 0, and R imports them as real numbers because they look like real numbers. R cannot protect you from that — but read_csv(na = c("", "NA", "N/A", "-999")) can, and a histogram will show you a spike at -999 that no amount of staring at a spreadsheet would have.

What it is really for

Most language design is about convenience. This piece of R's design is about not being wrong, and it is willing to be slightly inconvenient to achieve that — which is exactly the trade a statistical language should make.

The spreadsheet that quietly returns 4,710 is easier to use and will eventually let you publish a number that is wrong, with no indication anywhere that a decision was made. R stops, says it does not know, and makes you write down what you want done about it.

Six extra keystrokes, and a decision that is visible in the code forever. For a language built to produce numbers that other people will act on, that is the right default — and the fact that it annoys you in the first hour is a reasonable price for the analysis it saves you in the first year.

r missing data statistics data analysis data quality