# colSums: Form Row and Column Sums and Means In Matrix: Sparse and Dense Matrix Classes and Methods

## Description

Form row and column sums and means for objects, for `sparseMatrix` the result may optionally be sparse (`sparseVector`), too. Row or column names are kept respectively as for base matrices and `colSums` methods, when the result is `numeric` vector.

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17``` ```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) ```

## Arguments

 `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`. Only applicable when `x` is inheriting from a `sparseMatrix` class.

## Value

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

`dimnames(x)` are only kept (as `names(v)`) when the resulting `v` is `numeric`, since `sparseVector`s do not have names.

`colSums` and the `sparseVector` classes.

## Examples

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30``` ```(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) # 14 of 70 are not zero colMeans(MM, sparseResult = TRUE) # 16 of 80 are not zero ## Since we have no 'NA's, these two are equivalent : stopifnot(identical(rowMeans(MM, sparseResult = TRUE), rowMeans(MM, sparseResult = TRUE, na.rm = TRUE)), rowMeans(Diagonal(16)) == 1/16, colSums(Diagonal(7)) == 1) ## dimnames(x) --> names( ) : dimnames(M) <- list(paste0("r", 1:7), paste0("V",1:8)) M colSums(M) rowMeans(M) ## Assertions : stopifnot(all.equal(colSums(M), setNames(c(1,1,6,6,6,6,3,2), colnames(M))), all.equal(rowMeans(M), structure(c(1,1,4,8,12,3,2) / 8, .Names = paste0("r", 1:7)))) ```

### Example output

```7 x 8 sparse Matrix of class "dgCMatrix"

[1,] 1 . . . . . . .
[2,] . 1 . . . . . .
[3,] . . 1 1 1 1 . .
[4,] . . 2 2 2 2 . .
[5,] . . 3 3 3 3 . .
[6,] . . . . . . 3 .
[7,] . . . . . . . 2
[1] 1 1 6 6 6 6 3 2
Note: method with signature 'diagonalMatrix#Matrix' chosen for function 'kronecker',
target signature 'ddiMatrix#dgCMatrix'.
"ANY#sparseMatrix" would also be valid
Note: method with signature 'dsparseMatrix#dsparseMatrix' chosen for function 'kronecker',
target signature 'dtTMatrix#dgCMatrix'.
"TsparseMatrix#sparseMatrix" would also be valid
[1] 70 80
[1] 160
[1] 32
sparse vector (nnz/length = 16/80) of class "dsparseVector"
[1]  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . 10 10 60 60 60 60 30 20  .
[26]  .  .  .  .  .  .  .  2  2 12 12 12 12  6  4  .  .  .  .  .  .  .  .  .  .
[51]  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .
[76]  .  .  .  .  .
sparse vector (nnz/length = 14/70) of class "dsparseVector"
[1]   .   .   .   .   .   .   .   .   .   .   .   .   .   .  10  10  40  80 120
[20]  30  20   .   .   .   .   .   .   .   2   2   8  16  24   6   4   .   .   .
[39]   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .   .
[58]   .   .   .   .   .   .   .   .   .   .   .   .   .
sparse vector (nnz/length = 16/80) of class "dsparseVector"
[1]          .          .          .          .          .          .
[7]          .          .          .          .          .          .
[13]          .          .          .          . 0.14285714 0.14285714
[19] 0.85714286 0.85714286 0.85714286 0.85714286 0.42857143 0.28571429
[25]          .          .          .          .          .          .
[31]          .          . 0.02857143 0.02857143 0.17142857 0.17142857
[37] 0.17142857 0.17142857 0.08571429 0.05714286          .          .
[43]          .          .          .          .          .          .
[49]          .          .          .          .          .          .
[55]          .          .          .          .          .          .
[61]          .          .          .          .          .          .
[67]          .          .          .          .          .          .
[73]          .          .          .          .          .          .
[79]          .          .
7 x 8 sparse Matrix of class "dgCMatrix"
V1 V2 V3 V4 V5 V6 V7 V8
r1  1  .  .  .  .  .  .  .
r2  .  1  .  .  .  .  .  .
r3  .  .  1  1  1  1  .  .
r4  .  .  2  2  2  2  .  .
r5  .  .  3  3  3  3  .  .
r6  .  .  .  .  .  .  3  .
r7  .  .  .  .  .  .  .  2
V1 V2 V3 V4 V5 V6 V7 V8
1  1  6  6  6  6  3  2
r1    r2    r3    r4    r5    r6    r7
0.125 0.125 0.500 1.000 1.500 0.375 0.250
```

Matrix documentation built on Jan. 7, 2021, 1:08 a.m.