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)
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.
|
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 |
|
X | a numeric matrix. |
m | the first dimension of X matrix. |
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)