# colSums

0th

Percentile

##### Form Row and Column Sums and Means

Form row and column sums and means for numeric arrays (or data frames).

Keywords
arith, algebra, array
##### Usage
colSums (x, na.rm = FALSE, dims = 1)
rowSums (x, na.rm = FALSE, dims = 1)
colMeans(x, na.rm = FALSE, dims = 1)
rowMeans(x, na.rm = FALSE, dims = 1)
.colSums(x, m, n, na.rm = FALSE)
.rowSums(x, m, n, na.rm = FALSE)
.colMeans(x, m, n, na.rm = FALSE)
.rowMeans(x, m, n, na.rm = FALSE)
##### Arguments
x
an array of two or more dimensions, containing numeric, complex, integer or logical values, or a numeric data frame. For .colSums() etc, a numeric, integer or logical matrix (or vector of length m * n).
na.rm
logical. Should missing values (including NaN) be omitted from the calculations?
dims
integer: Which dimensions are regarded as ‘rows’ or ‘columns’ to sum over. For row*, the sum or mean is over dimensions dims+1, ...; for col* it is over dimensions 1:dims.
m, n
the dimensions of the matrix x for .colSums() etc.
##### Details

These functions are equivalent to use of apply with FUN = mean or FUN = sum with appropriate margins, but are a lot faster. As they are written for speed, they blur over some of the subtleties of NaN and NA. If na.rm = FALSE and either NaN or NA appears in a sum, the result will be one of NaN or NA, but which might be platform-dependent.

Notice that omission of missing values is done on a per-column or per-row basis, so column means may not be over the same set of rows, and vice versa. To use only complete rows or columns, first select them with na.omit or complete.cases (possibly on the transpose of x).

The versions with an initial dot in the name (.colSums() etc) are ‘bare-bones’ versions for use in programming: they apply only to numeric (like) matrices and do not name the result.

##### Value

A numeric or complex array of suitable size, or a vector if the result is one-dimensional. For the first four functions the dimnames (or names for a vector result) are taken from the original array.If there are no values in a range to be summed over (after removing missing values with na.rm = TRUE), that component of the output is set to 0 (*Sums) or NaN (*Means), consistent with sum and mean.

apply, rowsum

• colSums
• rowSums
• colMeans
• rowMeans
• .colSums
• .rowSums
• .colMeans
• .rowMeans
##### Examples
library(base) ## Compute row and column sums for a matrix: x <- cbind(x1 = 3, x2 = c(4:1, 2:5)) rowSums(x); colSums(x) dimnames(x)[] <- letters[1:8] rowSums(x); colSums(x); rowMeans(x); colMeans(x) x[] <- as.integer(x) rowSums(x); colSums(x) x[] <- x < 3 rowSums(x); colSums(x) x <- cbind(x1 = 3, x2 = c(4:1, 2:5)) x[3, ] <- NA; x[4, 2] <- NA rowSums(x); colSums(x); rowMeans(x); colMeans(x) rowSums(x, na.rm = TRUE); colSums(x, na.rm = TRUE) rowMeans(x, na.rm = TRUE); colMeans(x, na.rm = TRUE) ## an array dim(UCBAdmissions) rowSums(UCBAdmissions); rowSums(UCBAdmissions, dims = 2) colSums(UCBAdmissions); colSums(UCBAdmissions, dims = 2) ## complex case x <- cbind(x1 = 3 + 2i, x2 = c(4:1, 2:5) - 5i) x[3, ] <- NA; x[4, 2] <- NA rowSums(x); colSums(x); rowMeans(x); colMeans(x) rowSums(x, na.rm = TRUE); colSums(x, na.rm = TRUE) rowMeans(x, na.rm = TRUE); colMeans(x, na.rm = TRUE) 
Documentation reproduced from package base, version 3.3, License: Part of R @VERSION@

### Community examples

mark@niemannross.com at Dec 12, 2018 base v3.5.1

[Example file for LinkedIn Learning:](https://linkedin-learning.pxf.io/rweekly_colmeans) r # Description: Example file for mean, colMean, & rowMean # main idea: specialty functions vs apply - speed and clarity of code I.am.a.matrix <- matrix(1:30,nrow = 6) # need some data I.am.a.matrix # what does this look like? # calculating mean with apply ------------------------------------------------------- apply(I.am.a.matrix,1,mean) # equivalent of rowMeans(I.am.a.matrix) apply(I.am.a.matrix,2,mean) # equivalent of colMeans(I.am.a.matrix) # calculating mean with rowMeans ------------------------------------------ rowMeans(I.am.a.matrix) # means of all rows colMeans(I.am.a.matrix) # means of all columns # and other similar functions colSums(I.am.a.matrix) # sum of all columns