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 'row*', the sum or mean is over dimensions
          'dims+1, ...'; for 'col*' 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 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 'NA' ('*Means'), consistent
     with 'sum' and 'mean'.

_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)

