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()
- 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.) - Run
sum(x). The usage in the R manual issum(..., na.rm = FALSE), so the default keeps missing values in play. - If the data contain missing values, run
sum(x, na.rm = TRUE). The manual describesna.rmas answering “Should missing values (including NaN) be removed?”
Step 2: a running total with cumsum()
- Run
cumsum(x). It returns a vector of the same length whose elements are the cumulative sums of the elements of the argument. - Note that
cumsum()has nona.rmargument; its usage is simplycumsum(x). Remove or recode missing values first.
Step 3: totals across rows or columns with rowSums() and colSums()
- For a matrix or data frame
dfof numeric columns, runcolSums(df)for one total per column orrowSums(df)for one total per row. - Add
na.rm = TRUEto 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. - For arrays with more than two dimensions, the
dimsargument sets which dimensions are regarded as “rows” or “columns” to sum over (defaultdims = 1).
Step 4: totals by group
- dplyr:
df |> summarise(total = sum(score), .by = group). The dplyr reference gives the usage assummarise(.data, ..., .by = NULL, .groups = NULL), sayssummarise()creates a new data frame, and describes.byas an alternative togroup_by(). The older formdf |> group_by(group) |> summarise(total = sum(score))works the same way; the dplyr page shows this pattern withmean()andn(), andsum()slots into it identically. More on grouping in grouping in R and the pipe operator. - Base R:
rowsum(x, group)returns one row per unique value ofgroup, with column sums for each group. The manual’s usage isrowsum(x, group, reorder = TRUE, ...), and by default the rows are in the order ofsort(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
- R Core Team, R manual, sum (checked 2026-10-29).
- R Core Team, R manual, cumsum, cumprod, cummax, cummin (checked 2026-10-29).
- R Core Team, R manual, colSums, rowSums, colMeans, rowMeans (checked 2026-10-29).
- R Core Team, R manual, rowsum (checked 2026-10-29).
- dplyr reference, summarise (checked 2026-10-29).







