Skip to main content
v2026.11,986 entries · CC-BY 4.0

na.rm in R (rm.na): How to Ignore NA in mean(), sum() and sd()

na.rm (often searched as rm.na) tells R functions to drop NA values before calculating. Defaults, examples and which functions lack it.

Written and maintained by CASRAI Editorial Board

Published

On this page

In R, na.rm is an argument (often searched as “rm.na” or “na rm”) that tells a summary function whether to strip missing values (NA) before it calculates. It defaults to FALSE in mean(), sum(), median(), max(), min(), sd(), colSums() and rowMeans(), so a single NA makes the result NA; setting na.rm = TRUE drops the NAs first and summarises what is left. The spelling is always na.rm, never rm.na.

What na.rm means and when researchers need it

Research datasets almost always contain gaps: a skipped survey item, a failed assay well, a participant lost to follow-up. R stores these as NA. The R manual describes na.rm in mean() as “a logical evaluating to TRUE or FALSE indicating whether NA values should be stripped before the computation proceeds”, and the default is FALSE. The default means that if any input is unknown, the summary of all inputs is reported as unknown instead of quietly giving a number built from a subset.

That makes na.rm a methodological decision, not a typing detail. Setting it to TRUE means every function call analyses only the observed values, which is a complete-case style of handling for that single statistic. Whether that is defensible depends on why the values are missing, which no R argument can tell you. If missingness could bias your estimate, document the choice in your analysis plan and consider approaches beyond deletion; for removing whole rows rather than ignoring NAs inside one calculation, see our guide to omitting rows with NA in R, and for the opposite step of recoding, replacing NA with 0 in R.

Is it rm.na or na.rm?

It is na.rm: “NA” first, then “rm” for remove. The reversed form rm.na is a common slip, and R will not treat it as the same argument. A function such as mean(x, trim = 0, na.rm = FALSE, ...) has a ... in its signature, so a misspelt argument name is not necessarily an error, and the NA can stay in the result. Worse, sum() takes its inputs through ... itself (sum(..., na.rm = FALSE)), so a stray rm.na = TRUE is likely to be treated as one more value to add rather than as an option. Underscore or camel-case variants such as na_rm behave the same way in base R. If your output is still NA after you “added na.rm”, check the spelling first.

Install or access

Nothing needs installing beyond R itself. The functions below live in the base and stats packages, which are part of a standard R installation and attached when R starts. R is free software distributed through the Comprehensive R Archive Network at cran.r-project.org, and the function documentation quoted on this page is the R-devel manual at stat.ethz.ch/R-manual. Inside R, ?mean opens the same help page for your installed version.

Step-by-step: using na.rm in the common summary functions

The examples below use one small vector with a single missing value. Each result was worked by hand from the documented definitions: for the observed values 4, 8, 6 and 12, the sum is 30, the mean is 7.5 and the sorted middle pair 6 and 8 gives a median of 7.

  1. Create the data and see the default behaviour.
    x = c(4, 8, NA, 6, 12)mean(x)

    The result is NA, because na.rm is FALSE by default.

  2. Add na.rm = TRUE to the call.
    mean(x, na.rm = TRUE)

    The result is 7.5, the mean of the four observed values (30 divided by 4).

  3. Use the same argument in sum(). The signature is sum(..., na.rm = FALSE).
    sum(x)sum(x, na.rm = TRUE)

    The first returns NA; the second returns 30. The manual notes that without na.rm = TRUE any NA or NaN makes the result NA or NaN. See also our guide to summation in R.

  4. Use it in median(), whose signature is median(x, na.rm = FALSE, ...).
    median(x, na.rm = TRUE)

    The result is 7. Without it, the manual says the result is NA of the same type as x.

  5. Use it in max() and min(), signature max(..., na.rm = FALSE).
    max(x, na.rm = TRUE)min(x, na.rm = TRUE)

    The results are 12 and 4.

  6. Use it in sd(), signature sd(x, na.rm = FALSE).
    sd(x, na.rm = TRUE)

    The four observed values have a mean of 7.5 and squared deviations summing to 35; dividing by n – 1 = 3 and taking the square root gives about 3.416. Our guide to standard deviation in R covers the same trap in more detail.

  7. For matrices and data frames, use colSums(), colMeans(), rowSums() and rowMeans(). Their signature is colSums(x, na.rm = FALSE, dims = 1).
    m = matrix(c(2, 4, NA, 6, 1, NA, 3, 5), nrow = 4)colSums(m, na.rm = TRUE)rowMeans(m, na.rm = TRUE)

    Here column one holds 2, 4, NA, 6 and column two holds 1, NA, 3, 5. colSums returns 12 9, and rowMeans returns 1.5 4.0 3.0 5.5.

The data table below lists each function with its documented default and its documented behaviour with missing values.

Output and how to read it

Read the result against your count of NAs, not against the number you hoped to see. Three habits make that routine.

  • Count what was dropped. sum(is.na(x)) returns the number of NAs in x, and sum(!is.na(x)) the number that remain. Report the second as the n behind any mean or standard deviation computed with na.rm = TRUE.
  • Know that the removal is silent. na.rm = TRUE produces no message. A column that is 60% missing and a column that is 2% missing return equally confident-looking numbers.
  • Row and column summaries drop independently. The manual states for colSums() and its relatives that “omission of missing values is done on a per-column or per-row basis”. In the matrix above, the two column sums are built from three values each, while the row means are built from one or two. Different summaries in the same table can therefore rest on different subsets of participants.

Empty and all-NA inputs

After NAs are removed there may be nothing left, and the documented results differ by function. The manual for sum() states that “the sum of an empty set is zero, by definition”, so an all-NA column summed with na.rm = TRUE gives 0, which can look like real data. For max() and min() on numeric input the empty-set results are -Inf and +Inf respectively. For median(), the manual states that if there are no values the result is NA. For sd(), “the standard deviation of a length-one or zero-length vector is NA”. An Inf or a spurious 0 in a results table is a prompt to check that a group was not entirely missing.

Functions with no na.rm argument

Not every statistical function takes na.rm. cor() is the one researchers meet most. It has no na.rm; it uses a use argument instead, whose documented values are "everything" (the default, which propagates NA), "all.obs" (an error if any NA is present), "complete.obs" (casewise deletion, with an error if there are no complete cases), "na.or.complete" and "pairwise.complete.obs". The stats documentation adds that na.rm appears only in var, where it determines the default of use. For correlation in practice, write cor(x, y, use = "complete.obs"); our cor() in R guide covers the arguments. Passing na.rm = TRUE to cor() is not a documented route.

Troubleshooting

The result is still NA after adding na.rm = TRUE

Check, in order: the spelling (na.rm, not rm.na); that the argument is inside the right call (mean(x, na.rm = TRUE), not mean(x), na.rm = TRUE); that the function actually has the argument; and that the data are really stored as NA. A column read from a file with placeholder text such as “N/A”, “-” or “missing” is character data, not NA, and na.rm will not touch the placeholders.

pmax() and pmin() still return NA

The manual for pmax() and pmin() states that they return NA if all the parallel elements are NA, even when na.rm = TRUE.

The output is unchanged

A function whose signature has no na.rm may not warn you about it. Read the signature on the help page (?function_name) rather than assuming the argument exists.

A number you did not expect for an empty group

See the empty-input rules above: a sum of nothing is 0, a maximum of nothing is -Inf, and a median or standard deviation of nothing is NA. Count the non-missing values per group before reporting.

Alternatives to na.rm

R and its documentation are free, and R itself offers several ways to handle NAs besides na.rm. The table compares the options this page’s sources document.

Approach Where it applies What it does, per the R manual
na.rm = TRUE mean, sum, median, max, min, sd, colSums, rowMeans Strips NA values before the computation proceeds
use = "complete.obs" cor, and other functions with a use argument Casewise deletion; error if there are no complete cases
use = "pairwise.complete.obs" cor Uses the cases complete for each pair of variables
use = "everything" (default) cor NA values propagate, so affected results are NA
Remove rows first Whole data frame Not an argument; see omit rows with NA in R

Frequently asked questions

What does na.rm mean in R?

It stands for “NA remove”. It is a logical argument, TRUE or FALSE, that says whether NA values should be stripped before the computation proceeds. The default in the functions on this page is FALSE.

Is it rm.na or na.rm in R?

The argument is na.rm. rm.na is not an argument of these functions, and a misspelt name can fail to act as an option, leaving the NA in place.

Why does mean(x) return NA?

Because x contains at least one NA and na.rm defaults to FALSE. Use mean(x, na.rm = TRUE) if analysing only the observed values is appropriate for your data.

Does na.rm = TRUE work in cor()?

No. cor() has no na.rm argument; use its use argument, for example use = "complete.obs".

Is na.rm = TRUE statistically safe?

It is only as safe as your reason for the data being missing. It makes each statistic use the available values and gives no warning, so report how many values were dropped and justify the choice in your methods.

What does max() return when every value is NA and na.rm = TRUE?

For numeric input the documented empty-set result of max() is -Inf (and +Inf for min()), so check for groups with no observed values.

Sources

Data: na.rm defaults and missing-value behaviour of common R summary functions

Compares, for eight common R functions, whether they have an na.rm argument, its default, and the documented result when NA values are present or the input is empty. Compiled by reading each function's page in the R-devel manual; every cell paraphrases the manual text for that function.

na.rm defaults and missing-value behaviour of common R summary functions
FunctionSignature as documentedna.rm defaultDocumented behaviour with NA or empty input
meanmean(x, trim = 0, na.rm = FALSE, ...)FALSEna.rm: should NA values be stripped before the computation proceeds
sumsum(..., na.rm = FALSE)FALSEWithout na.rm = TRUE any NA or NaN gives NA or NaN; the sum of an empty set is zero
medianmedian(x, na.rm = FALSE, ...)FALSEWith no values, or NA present and na.rm = FALSE, the result is NA of the same type as x
max / minmax(..., na.rm = FALSE)FALSENumeric empty set: max gives -Inf, min gives +Inf; pmax/pmin return NA if all parallel elements are NA even with na.rm = TRUE
sdsd(x, na.rm = FALSE)FALSEThe standard deviation of a length-one or zero-length vector is NA
colSums / rowMeanscolSums(x, na.rm = FALSE, dims = 1)FALSEOmission of missing values is done on a per-column or per-row basis
corcor(x, y, use = ...)No na.rm argumentUses use=: everything (default, NA), all.obs (error), complete.obs (casewise deletion), na.or.complete, pairwise.complete.obs

Sources

Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · full access

Ask about na.rm in R (rm.na): How to Ignore NA in mean(), sum() and sd()

Full access: large monthly usage, up to 10 source passages behind each answer, and MCP and API access for Claude Code, Codex and other CLIs. Cancel any time.

An AI assistant for research: funder rules, compliance, methods and publishing. It links the sources behind its answers and labels answers that come from a web search.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays open to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →