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

Summation in R: sum(), cumsum(), rowSums() and colSums()

Summation in R with sum(), cumsum(), rowSums(), colSums() and group sums, plus how each handles NA values, from the R manual.

Written and maintained by CASRAI Editorial Board

Published · Last updated

On this page

Summation in R is done with sum() for one total, cumsum() for a running total, rowSums() and colSums() for totals across a matrix or data frame, and summarise() from dplyr or rowsum() from base R for totals by group. The part that trips people up in research data is not the syntax but what each function does with missing values, and that differs between them.

What it is used for in research

Summing is the step behind item totals on a questionnaire scale, counts of events per participant, cumulative enrolment over time, and group totals before a table is built. Because an NA can silently turn a total into NA (or, in one case, into 0), the choice of function matters for the reported result. For related data-preparation steps see omitting rows with NA in R and replacing NA with 0 in R.

Install or access

sum(), cumsum(), rowSums(), colSums() and rowsum() are part of base R, so nothing needs to be installed. summarise() belongs to the dplyr package, installed once with install.packages("dplyr") and loaded with library(dplyr). The R manual at stat.ethz.ch and the dplyr reference at dplyr.tidyverse.org are linked under Sources.

Step-by-step: choosing and running the right sum

Step 1: one total with sum()

  1. Put the values in a numeric vector, for example x <- c(2, 4, 6). (If vectors are new to you, see what a vector is in R.)
  2. Run sum(x). The usage in the R manual is sum(..., na.rm = FALSE), so the default keeps missing values in play.
  3. If the data contain missing values, run sum(x, na.rm = TRUE). The manual describes na.rm as answering “Should missing values (including NaN) be removed?”

Step 2: a running total with cumsum()

  1. Run cumsum(x). It returns a vector of the same length whose elements are the cumulative sums of the elements of the argument.
  2. Note that cumsum() has no na.rm argument; its usage is simply cumsum(x). Remove or recode missing values first.

Step 3: totals across rows or columns with rowSums() and colSums()

  1. For a matrix or data frame df of numeric columns, run colSums(df) for one total per column or rowSums(df) for one total per row.
  2. Add na.rm = TRUE to omit missing values. The manual states that omission is done on a per-column or per-row basis, so a missing value in one column does not remove the whole row from the other columns’ totals.
  3. For arrays with more than two dimensions, the dims argument sets which dimensions are regarded as “rows” or “columns” to sum over (default dims = 1).

Step 4: totals by group

  1. dplyr: df |> summarise(total = sum(score), .by = group). The dplyr reference gives the usage as summarise(.data, ..., .by = NULL, .groups = NULL), says summarise() creates a new data frame, and describes .by as an alternative to group_by(). The older form df |> group_by(group) |> summarise(total = sum(score)) works the same way; the dplyr page shows this pattern with mean() and n(), and sum() slots into it identically. More on grouping in grouping in R and the pipe operator.
  2. Base R: rowsum(x, group) returns one row per unique value of group, with column sums for each group. The manual’s usage is rowsum(x, group, reorder = TRUE, ...), and by default the rows are in the order of sort(unique(group)).

Output and how to read it

The table sets out what each function returns, and what it does with a missing value. Every entry comes from the R manual pages cited under Sources.

Function Returns With an NA in the input
sum(x) One value The manual says an NA or NaN in any argument causes NA or NaN to be returned; na.rm = TRUE removes them
cumsum(x) A vector, one running total per element An NA in x causes the corresponding and following elements of the result to be NA
colSums(df) / rowSums(df) One total per column / per row Kept by default; na.rm = TRUE omits them per column or row
rowsum(x, group) One row of column sums per group Kept by default; na.rm = TRUE discards them (data frame and default methods)

A small example makes the first two rows concrete. With x <- c(1, 2, NA, 4), sum(x) is NA, sum(x, na.rm = TRUE) is 7, and cumsum(x) is 1 3 NA NA: the running total is lost from the missing value onward, which is why a cumulative curve with one gap goes blank for the rest of the series.

Integer or double?

The manual says the sum is integer when possible and double otherwise, and that integer overflow in sum() should no longer happen since R 3.5.0. Two functions still behave differently: for cumsum(), integer overflow gives NA with a warning, and for rowsum(), over- or underflow of integer arguments results in NA.

Which function when

You want Use Source for the behaviour
One grand total sum() R manual, sum
A running total cumsum() R manual, cumsum
A total per column or per row colSums(), rowSums() R manual, colSums
A total per group, as a data frame summarise() with .by or group_by() dplyr reference, summarise
Column sums per group, base R only rowsum() R manual, rowsum

Troubleshooting

My sum() returns NA

At least one value is NA or NaN. Use sum(x, na.rm = TRUE) if dropping those values is appropriate for your analysis, and report that you did so.

colSums() returns 0 for a column that is entirely missing

With na.rm = TRUE, if nothing is left after removing missing values, the manual says the result is 0 for the *Sums functions and NaN for the *Means functions. A column of all NA therefore sums to 0, not NA, so check for all-missing columns before treating a zero as a real total.

My cumulative total turns to NA part-way through

That is documented behaviour: an NA in x makes the corresponding and following elements NA. Handle the missing value before calling cumsum().

Results look slightly off when values have mixed signs

The sum() manual notes that loss of accuracy can occur when summing values of different signs, and that extended-precision accumulators are used where possible.

Free alternatives

Option Cost and access Best for
Base R functions (sum, cumsum, colSums, rowSums, rowsum) Included with R; nothing to install Totals, running totals, row/column totals, and grouped column sums without extra packages
dplyr summarise() Free package; installed with install.packages("dplyr") Group totals returned as a new data frame

FAQ

How do I sum a column in R?

Use sum(df$column) for one column, or colSums(df) for every numeric column at once.

How do I sum in R ignoring NA?

Add na.rm = TRUE: sum(x, na.rm = TRUE). The same argument exists on colSums(), rowSums() and rowsum(), but not on cumsum().

How do I get a running total in R?

Use cumsum(x). It returns one cumulative sum per element. A related cumulative function, cumprod(), is covered in factorials in R.

How do I sum by group in R?

With dplyr, summarise(total = sum(value), .by = group); in base R, rowsum(value, group). See grouping in R for the wider pattern.

Sources

Follow CASRAI

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

Ask CASRAI · free to try

Ask about Summation in R: sum(), cumsum(), rowSums() and colSums()

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

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 free 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 →