Convenience functions for adding or multiplying each row / column of a matrix by a number.
Examples
set.seed(12345)
mat <- matrix(rpois(40, lambda = 5), nrow = 4)
rownames(mat) <- paste0("gene", 1:4)
mat <- mat %>% as("dgCMatrix")
mat
#> 4 x 10 sparse Matrix of class "dgCMatrix"
#>
#> gene1 6 5 6 6 4 5 6 3 3 8
#> gene2 8 3 11 . 4 4 4 5 6 8
#> gene3 6 4 1 4 3 9 6 7 4 6
#> gene4 8 5 3 5 9 6 5 . 4 3
mat <- mat %>% as("IterableMatrix")
#######################################################################
## add_rows() example
#######################################################################
add_rows(mat, 1:4) %>% as("dgCMatrix")
#> 4 x 10 sparse Matrix of class "dgCMatrix"
#>
#> gene1 7 6 7 7 5 6 7 4 4 9
#> gene2 10 5 13 2 6 6 6 7 8 10
#> gene3 9 7 4 7 6 12 9 10 7 9
#> gene4 12 9 7 9 13 10 9 4 8 7
#######################################################################
## add_cols() example
#######################################################################
add_cols(mat, 1:10) %>% as("dgCMatrix")
#> 4 x 10 sparse Matrix of class "dgCMatrix"
#>
#> gene1 7 7 9 10 9 11 13 11 12 18
#> gene2 9 5 14 4 9 10 11 13 15 18
#> gene3 7 6 4 8 8 15 13 15 13 16
#> gene4 9 7 6 9 14 12 12 8 13 13
#######################################################################
## multiply_rows() example
#######################################################################
multiply_rows(mat, 1:4) %>% as("dgCMatrix")
#> 4 x 10 sparse Matrix of class "dgCMatrix"
#>
#> gene1 6 5 6 6 4 5 6 3 3 8
#> gene2 16 6 22 . 8 8 8 10 12 16
#> gene3 18 12 3 12 9 27 18 21 12 18
#> gene4 32 20 12 20 36 24 20 . 16 12
#######################################################################
## multiply_cols() example
#######################################################################
multiply_cols(mat, 1:10) %>% as("dgCMatrix")
#> 4 x 10 sparse Matrix of class "dgCMatrix"
#>
#> gene1 6 10 18 24 20 30 42 24 27 80
#> gene2 8 6 33 . 20 24 28 40 54 80
#> gene3 6 8 3 16 15 54 42 56 36 60
#> gene4 8 10 9 20 45 36 35 . 36 30