colSums                 package:base                 R Documentation

_F_o_r_m _R_o_w _a_n_d _C_o_l_u_m_n _S_u_m_s _a_n_d _M_e_a_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Form row and column sums and means for numeric arrays.

_U_s_a_g_e:

     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)

_A_r_g_u_m_e_n_t_s:

       x: an array of two or more dimensions, containing numeric,
          complex, integer or logical values, or a numeric data frame.

   na.rm: logical. Should missing values (including `NaN') be omitted
          from the calculations?

    dims: Which dimensions are regarded as ``rows'' or ``columns'' to
          sum over.  For `col*', the sum or mean is over dimensions
          `dims+1, ...'; for `row*' it is over dimensions `1:dims'.

_D_e_t_a_i_l_s:

     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.

_V_a_l_u_e:

     A numeric or complex array of suitable size, or a vector if the
     result is one-dimensional.  The `dimnames' (or `names' for a
     vector result) are taken from the original array.

     If there are no non-missing values in a range to be summed over,
     the component of the output is set to `NA'.

_S_e_e _A_l_s_o:

     `apply', `rowsum'

_E_x_a_m_p_l_e_s:

     ## Compute row and column sums for a matrix:
     x <- cbind(x1 = 3, x2 = c(4:1, 2:5))
     rowSums(x); colSums(x)
     dimnames(x)[[1]] <- 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
     data(UCBAdmissions)
     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)

