colMeans
Row and Column Summaries

### Description

Returns the mean or the sum for the specified columns, rows, or dimensions of arrays.

### Usage

    colMeans(x, na.rm = FALSE, dims = 1L, weights = NULL, freq = NULL,
       n = NULL)
    colSums(x, na.rm = FALSE, dims = 1L, weights = NULL, freq = NULL,
       n = NULL)
    rowMeans(x, na.rm = FALSE, dims = 1L, weights = NULL, freq = NULL,
       n = NULL)
    rowSums(x, na.rm = FALSE, dims = 1L, weights = NULL, freq = NULL,
       n = NULL)
    .colMeans(X, m, n, na.rm = FALSE)
    .rowMeans(X, m, n, na.rm = FALSE)
    .colSums(X, m, n, na.rm = FALSE)
    .rowSums(X, m, n, na.rm = FALSE)

### Arguments

x | a matrix, a data frame, an array, or a numeric vector.
na.rm | a logical value. Specifies how to handle missing values (NAs) in x. If TRUE, missing values are omitted from the calculations. If FALSE (the default), missing values in the input result in missing values in the corresponding elements of the output.
dims | an integer value that specifies the number of dimensions to treat as rows. For example, if x is an array with more than two dimensions (say five), dims determines what dimensions are summarized; if dims = 3, then rowMeans is a three-dimensional array consisting of the means across the remaining two dimensions, and colMeans is a two-dimensional array consisting of the means across the last three dimensions.
weights | a numeric vector that has the same number of observations as x. If x is a matrix, the number of rows for rowMeans or columns for colmeans.
freq | a numeric vector that consists of positive integers with the same number of observations as x. If present, the kth row of x is repeated k times. The effect is similar to the weights argument.
n | for the first four functions, an integer that specifies the number of rows. If supplied, this overrides the actual number of rows in x. This is useful for obtaining summaries on regular subsets of the data. for the last four functions, the second dimension of X matrix.

X | a numeric matrix.
m | the first dimension of X matrix.

### Details

The primary use of n is to compute summaries for a vector without first turning it into an array.
Value
returns the means or sums by row or column. Generally, the return values are contained in a vector, but if x is an array and the value of dims implies that the result has at least two dimensions, then the container is a matrix or an array.
If you specify n, then a vector without names is returned (and dims is ignored). Otherwise, if x contains names or dimnames, the result also contains names or dimnames.
Differences between Spotfire Enterprise Runtime for R and Open-source R

- Spotfire Enterprise Runtime for R accepts vectors, but open-source R does not.
- In Spotfire Enterprise Runtime for R, rowMeans/colMeans return NaN if there are no values; open-source R returns NA: rowMeans(matrix(NA), na.rm=T)
- colVars, colStdevs, rowVars, and rowStdevs are not supported by open-source R, but they are in Spotfire Enterprise Runtime for R.
- colSums, rowSums, colMeans and rowMeans are implemented both in open-source R and Spotfire Enterprise Runtime for R, but there are more arguments in the Spotfire Enterprise Runtime for R implementation (for example, weights, freq and n. All of these might not be presented).
- colSums, rowSums, colMeans and rowMeans are NOT generic functions in open-source R.

See Also
apply, is.na, is.nan, mean, stdev, sum, var.
Examples

    x <- matrix(1:12, 4)
    rowMeans(x)
    colMeans(x)
    # Summaries for regular subsets of a vector
    # Do not run on R
    x <- 1:10
    colMeans(x, n=5)           # groups of 5 consecutive observations
    rowMeans(x, n=5)           # groups of every fifth observation
    # Higher-dimensional array
    x <- array(runif(24), dim=c(2,3,4))
    rowMeans(x)                  # vector of length 2.
    rowMeans(x, dims=2)          # 2x3 matrix.
    apply(x, 1:2, mean)          # same as previous
    colMeans(x)                  # 3x4 matrix.
    colMeans(x, dims=2)          # vector of length 4.
    colMeans(aperm(x, c(2,1,3))) # 2x4 matrix
    # Bootstrap the sample mean
    y <- runif(10)
    indices <- sample(1:10, 10*1000, replace=TRUE) # 1000 samples
    # One way -- make use of the argument "n"
    # Do not run on R
    colMeans(y[indices], n=10)
    # Alternative (slower)
    boot.y <- y[indices]
    dim(boot.y) <- c(10, 1000)
    colMeans(boot.y)
    # Same as previous, but much slower
    apply(boot.y, 2, mean)
    # .colSums, .colMeans, .rowSums, .rowMeans
    X <- matrix(1:12, nrow = 3)
    .colSums(X, 3, 4)
    .rowMeans(X, 3, 4)
    X[2, 3] <- NA
    X[1, 4] <-NA
    .colMeans(X, 3, 4)
    .colMeans(X, 3, 4, TRUE)
    .rowSums(X, 3, 4)
    .rowSums(X, 3, 4, TRUE)

Package base version 6.1.5-18
Package Index