Form row and column sums and means for numeric arrays (or data frames).
1 2 3 4 5 6 7 8 9
an array of two or more dimensions, containing numeric,
complex, integer or logical values, or a numeric data frame. For
logical. Should missing values (including
integer: Which dimensions are regarded as ‘rows’ or
‘columns’ to sum over. For
the dimensions of the matrix
These functions are equivalent to use of
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
FALSE and either
NA appears in a sum, the
result will be one of
NA, but which might be
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
(possibly on the transpose of
The versions with an initial dot in the name (
are ‘bare-bones’ versions for use in programming: they apply
only to numeric (like) matrices and do not name the result.
A numeric or complex array of suitable size, or a vector if the result
is one-dimensional. For the first four functions the
names for a vector result) are taken from the original
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
*Means), consistent with
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
## 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)
We want your feedback!
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.