colSums                package:Matrix                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 'Matrix' objects.

_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, ...)

     ## S4 method for signature 'CsparseMatrix':
     colSums(x, na.rm = FALSE,
             dims = 1, sparseResult = FALSE)
     ## S4 method for signature 'CsparseMatrix':
     rowSums(x, na.rm = FALSE,
             dims = 1, sparseResult = FALSE)
     ## S4 method for signature 'CsparseMatrix':
     colMeans(x, na.rm = FALSE,
             dims = 1, sparseResult = FALSE)
     ## S4 method for signature 'CsparseMatrix':
     rowMeans(x, na.rm = FALSE,
             dims = 1, sparseResult = FALSE)

_A_r_g_u_m_e_n_t_s:

       x: a Matrix, i.e., inheriting from  'Matrix'.

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

    dims: completely ignored by the 'Matrix' methods.

     ...: potentially further arguments, for method '<->' generic
          compatibility.

sparseResult: logical indicating if the result should be sparse, i.e.,
          inheriting from class 'sparseVector'.

_V_a_l_u_e:

     returns a numeric vector if 'sparseResult' is 'FALSE' as per
     default.  Otherwise, returns a 'sparseVector'.

_S_e_e _A_l_s_o:

     'colSums' and the 'sparseVector' classes.

_E_x_a_m_p_l_e_s:

     (M <- bdiag(Diagonal(2), matrix(1:3, 3,4), diag(3:2))) # 7 x 8
     colSums(M)
     d <- Diagonal(10, c(0,0,10,0,2,rep(0,5)))
     MM <- kronecker(d, M)
     dim(MM) # 70 80
     length(MM@x) # 160, but many are '0' ; drop those:
     MM <- drop0(MM)
     length(MM@x) # 32
       cm <- colSums(MM)
     (scm <- colSums(MM, sparseResult = TRUE))
     stopifnot(is(scm, "sparseVector"),
               identical(cm, as.numeric(scm)))
     rowSums(MM, sparseResult = TRUE) # 16 of 70 are not zero
     colMeans(MM, sparseResult = TRUE)
     ## Since we have no 'NA's, these two are equivalent :
     stopifnot(identical(rowMeans(MM, sparseResult = TRUE),
                         rowMeans(MM, sparseResult = TRUE, na.rm = TRUE)))

