├── src ├── .gitignore ├── types.h ├── SparseMatrixView.h ├── SparseMatrixView.cpp ├── my_utils.h ├── ColumnView.h ├── VectorSubsetView.h ├── SkipNAVectorSubsetView.h ├── quantile.h ├── sample_rank.h └── row_methods.cpp ├── vignettes ├── .gitignore └── sparseMatrixStats.Rmd ├── LICENSE ├── .Rbuildignore ├── man ├── figures │ └── logo.png ├── xgCMatrix-class.Rd ├── colCumsums-xgCMatrix-method.Rd ├── colCumprods-xgCMatrix-method.Rd ├── colAnyNAs-xgCMatrix-method.Rd ├── colCummaxs-dgCMatrix-method.Rd ├── colCummins-dgCMatrix-method.Rd ├── colCollapse-xgCMatrix-method.Rd ├── colMaxs-dgCMatrix-method.Rd ├── colMins-dgCMatrix-method.Rd ├── colMedians-dgCMatrix-method.Rd ├── colSums2-xgCMatrix-method.Rd ├── colTabulates-xgCMatrix-method.Rd ├── colIQRs-xgCMatrix-method.Rd ├── colDiffs-dgCMatrix-method.Rd ├── colLogSumExps-xgCMatrix-method.Rd ├── colOrderStats-dgCMatrix-method.Rd ├── colMeans2-xgCMatrix-method.Rd ├── colProds-xgCMatrix-method.Rd ├── colAlls-xgCMatrix-method.Rd ├── colAnys-xgCMatrix-method.Rd ├── colSds-xgCMatrix-method.Rd ├── colVars-xgCMatrix-method.Rd ├── colWeightedMeans-xgCMatrix-method.Rd ├── colWeightedVars-xgCMatrix-method.Rd ├── colCounts-xgCMatrix-method.Rd ├── colWeightedSds-xgCMatrix-method.Rd ├── colWeightedMedians-dgCMatrix-method.Rd ├── colVarDiffs-dgCMatrix-method.Rd ├── colSdDiffs-dgCMatrix-method.Rd ├── colIQRDiffs-dgCMatrix-method.Rd ├── colMads-dgCMatrix-method.Rd ├── colQuantiles-xgCMatrix-method.Rd ├── colMadDiffs-dgCMatrix-method.Rd ├── colRanges-dgCMatrix-method.Rd ├── colWeightedMads-dgCMatrix-method.Rd ├── colAvgsPerRowSet-xgCMatrix-method.Rd └── colRanks-dgCMatrix-method.Rd ├── tests ├── testthat.R └── testthat │ ├── test-quantile_sparse.R │ ├── setup.R │ ├── test-logicals.R │ └── test-row_functions.R ├── .gitignore ├── R ├── AllClasses.R ├── sparseMatrixStats-package.R ├── utils.R ├── sparse_matrix_iterator.R ├── RcppExports.R └── methods_row.R ├── DESCRIPTION ├── NAMESPACE ├── NEWS ├── .github └── workflows │ ├── R-CMD-check-release.yaml │ └── R-CMD-check-devel.yaml └── README.Rmd /src/.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.dll 4 | -------------------------------------------------------------------------------- /vignettes/.gitignore: -------------------------------------------------------------------------------- 1 | *.html 2 | *.R 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | YEAR: 2020 2 | COPYRIGHT HOLDER: Constantin Ahlmann-Eltze 3 | -------------------------------------------------------------------------------- /.Rbuildignore: -------------------------------------------------------------------------------- 1 | ^.*\.Rproj$ 2 | ^\.Rproj\.user$ 3 | ^README\.Rmd$ 4 | .github 5 | ^LICENSE\.md$ 6 | -------------------------------------------------------------------------------- /man/figures/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/const-ae/sparseMatrixStats/HEAD/man/figures/logo.png -------------------------------------------------------------------------------- /tests/testthat.R: -------------------------------------------------------------------------------- 1 | library(testthat) 2 | library(sparseMatrixStats) 3 | 4 | test_check("sparseMatrixStats") 5 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *.rda 4 | *.RData 5 | .Rproj.user 6 | .Rhistory 7 | sparseMatrixStats.Rproj 8 | inst/doc 9 | -------------------------------------------------------------------------------- /R/AllClasses.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | #' Union of double and logical column-sparse matrices 4 | #' 5 | #' Union of dgCMatrix and lgCMatrix 6 | #' 7 | #' 8 | #' @importClassesFrom Matrix dgCMatrix lgCMatrix 9 | setClassUnion("xgCMatrix", members = c("dgCMatrix", "lgCMatrix")) 10 | 11 | -------------------------------------------------------------------------------- /man/xgCMatrix-class.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/AllClasses.R 3 | \docType{class} 4 | \name{xgCMatrix-class} 5 | \alias{xgCMatrix-class} 6 | \title{Union of double and logical column-sparse matrices} 7 | \description{ 8 | Union of dgCMatrix and lgCMatrix 9 | } 10 | -------------------------------------------------------------------------------- /tests/testthat/test-quantile_sparse.R: -------------------------------------------------------------------------------- 1 | test_that("quantile_sparse method works", { 2 | for(i in 1:100){ 3 | N <- rpois(1, lambda=2) 4 | vec <- rnorm(N) 5 | nz <- rpois(1, lambda=3) 6 | compl_vec <- sort(c(vec, rep(0, nz))) 7 | prob <- runif(1) 8 | q1 <- quantile(compl_vec, prob) 9 | q2 <- quantile_sparse(vec, nz, prob) 10 | testthat::expect_equal(unname(q1), q2) 11 | } 12 | }) 13 | -------------------------------------------------------------------------------- /src/types.h: -------------------------------------------------------------------------------- 1 | #ifndef types_h 2 | #define types_h 3 | 4 | 5 | #include /* R_xlen_t, ... */ 6 | 7 | 8 | 9 | 10 | 11 | /* As in /src/include/Defn.h */ 12 | #ifdef HAVE_LONG_DOUBLE 13 | #define LDOUBLE long double 14 | #else 15 | #define LDOUBLE double 16 | #endif 17 | 18 | 19 | /* Macro to check for user interrupts every 2^20 iteration */ 20 | #define R_CHECK_USER_INTERRUPT(i) if (i % 1048576 == 0) R_CheckUserInterrupt() 21 | 22 | 23 | 24 | #endif /* types_h*/ 25 | -------------------------------------------------------------------------------- /R/sparseMatrixStats-package.R: -------------------------------------------------------------------------------- 1 | ## usethis namespace: start 2 | #' @importFrom Rcpp sourceCpp 3 | ## usethis namespace: end 4 | NULL 5 | 6 | # NOTE: Import a single function to quieten R CMD check NOTE: 7 | # Package in Depends field not imported from: ‘matrixStats’ 8 | # These packages need to be imported from (in the NAMESPACE file) 9 | # for when this namespace is loaded but not attached. 10 | #' @importFrom matrixStats allocArray 11 | NULL 12 | 13 | 14 | #' @useDynLib sparseMatrixStats 15 | NULL 16 | 17 | #' @import methods 18 | NULL 19 | 20 | #' @importFrom Matrix t 21 | NULL 22 | 23 | #' @import MatrixGenerics 24 | NULL 25 | 26 | #' @importFrom stats setNames 27 | NULL 28 | -------------------------------------------------------------------------------- /src/SparseMatrixView.h: -------------------------------------------------------------------------------- 1 | #ifndef SparseMatrixView_h 2 | #define SparseMatrixView_h 3 | 4 | #include 5 | using namespace Rcpp; 6 | 7 | class dgCMatrixView { 8 | 9 | public: 10 | const R_len_t nrow; 11 | const R_len_t ncol; 12 | const NumericVector values; 13 | const IntegerVector row_indices; 14 | const IntegerVector col_ptrs; 15 | 16 | dgCMatrixView(R_len_t nrow_, R_len_t ncol_, const NumericVector values_, const IntegerVector row_indices_, const IntegerVector col_ptrs_): 17 | nrow(nrow_), ncol(ncol_), values(values_), row_indices(row_indices_), col_ptrs(col_ptrs_) {} 18 | 19 | }; 20 | 21 | dgCMatrixView wrap_dgCMatrix(Rcpp::S4 sp_mat); 22 | 23 | #endif /* SparseMatrixView_h */ 24 | -------------------------------------------------------------------------------- /DESCRIPTION: -------------------------------------------------------------------------------- 1 | Package: sparseMatrixStats 2 | Type: Package 3 | Title: Summary Statistics for Rows and Columns of Sparse Matrices 4 | Version: 1.19.0 5 | Authors@R: person("Constantin", "Ahlmann-Eltze", email = "artjom31415@googlemail.com", 6 | role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3762-068X")) 7 | Description: High performance functions for row and column operations on sparse matrices. 8 | For example: col / rowMeans2, col / rowMedians, col / rowVars etc. Currently, 9 | the optimizations are limited to data in the column sparse format. 10 | This package is inspired by the matrixStats package by Henrik Bengtsson. 11 | License: MIT + file LICENSE 12 | Encoding: UTF-8 13 | LazyData: true 14 | RoxygenNote: 7.3.1 15 | LinkingTo: 16 | Rcpp 17 | Imports: 18 | Rcpp, 19 | Matrix, 20 | matrixStats (>= 0.60.0), 21 | methods 22 | Depends: 23 | MatrixGenerics (>= 1.5.3) 24 | Suggests: 25 | testthat (>= 2.1.0), 26 | knitr, 27 | bench, 28 | rmarkdown, 29 | BiocStyle 30 | SystemRequirements: C++11 31 | VignetteBuilder: knitr 32 | URL: https://github.com/const-ae/sparseMatrixStats 33 | BugReports: https://github.com/const-ae/sparseMatrixStats/issues 34 | biocViews: Infrastructure, Software, DataRepresentation 35 | -------------------------------------------------------------------------------- /tests/testthat/setup.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | make_matrix <- function(nrow, ncol, frac_zero = 0.8, frac_na = 0){ 4 | n_total <- ncol * nrow 5 | data <- rnorm(n = n_total, mean=0, sd=10) 6 | data[sample(seq_len(n_total), size = round(n_total * frac_zero))] <- 0 7 | data[sample(seq_len(n_total), size = round(n_total * frac_na))] <- NA 8 | matrix(data, nrow=nrow, ncol=ncol) 9 | } 10 | 11 | make_matrix_with_all_features <- function(nrow, ncol){ 12 | if(ncol < 8){ 13 | stop("Too few columns") 14 | } 15 | mat <- make_matrix(nrow, ncol, frac_zero = 0.8) 16 | # All observed 17 | mat[,1] <- rnorm(nrow) 18 | # All zero 19 | mat[,2] <- 0 20 | # Some missing 21 | mat[7, 3] <- NA 22 | # All missing 23 | mat[,4] <- NA 24 | # Some infinite 25 | mat[1, 5] <- Inf 26 | mat[3, 6] <- -Inf 27 | # Some with known value 28 | mat[1, 7] <- 42 29 | 30 | mat 31 | } 32 | 33 | make_matrix_with_all_features_with_explicit_zeros <- function(nrow, ncol, frac_zero = 0.7){ 34 | i <- c(sample.int(nrow, round(nrow * ncol * (1-frac_zero)), replace = TRUE), 4, 1, 2) 35 | j <- c(sample.int(ncol, round(nrow * ncol * (1-frac_zero)), replace = TRUE), 1, 3, 2) 36 | x <- c(rpois(n = round(nrow * ncol * (1-frac_zero)), lambda = 0.5), NA, NA, -1) 37 | Matrix::sparseMatrix(i = i, j = j, x = x, dims = c(nrow, ncol)) 38 | } 39 | 40 | -------------------------------------------------------------------------------- /src/SparseMatrixView.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SparseMatrixView.h" 3 | #include "VectorSubsetView.h" 4 | using namespace Rcpp; 5 | 6 | // [[Rcpp::plugins("cpp11")]] 7 | 8 | dgCMatrixView wrap_dgCMatrix(Rcpp::S4 sp_mat){ 9 | Rcpp::IntegerVector dim = sp_mat.slot("Dim"); 10 | Rcpp::NumericVector values = sp_mat.slot("x"); 11 | R_len_t nrows = dim[0]; 12 | R_len_t ncols = dim[1]; 13 | 14 | Rcpp::IntegerVector row_indices = sp_mat.slot("i"); 15 | Rcpp::IntegerVector col_ptrs = sp_mat.slot("p"); 16 | return dgCMatrixView(nrows, ncols, values, row_indices, col_ptrs); 17 | } 18 | 19 | 20 | // // [[Rcpp::export]] 21 | // void print_matrix(Rcpp::S4 matrix){ 22 | // auto sp_mat = wrap_dgCMatrix(matrix); 23 | // 24 | // Rcout << "Hello Rcpp" << std::endl; 25 | // Rcout << sp_mat.ncol << std::endl; 26 | // Rcout << sp_mat.values.at(0) << std::endl; 27 | // Rcout << &sp_mat.values.get_ref().at(0) << std::endl; 28 | // } 29 | 30 | 31 | 32 | // You can include R code blocks in C++ files processed with sourceCpp 33 | // (useful for testing and development). The R code will be automatically 34 | // run after the compilation. 35 | // 36 | 37 | /*** R 38 | library(Matrix) 39 | source("../tests/testthat/setup.R") 40 | mat <- make_matrix_with_all_features(10, 5) 41 | mat 42 | sp_mat <- as(mat, "dgCMatrix") 43 | sp_mat 44 | 45 | print_matrix(sp_mat) 46 | */ 47 | -------------------------------------------------------------------------------- /src/my_utils.h: -------------------------------------------------------------------------------- 1 | #ifndef my_utils_h 2 | #define my_utils_h 3 | 4 | #include 5 | #include "types.h" 6 | 7 | template 8 | inline double sum_stable(Iterator iter){ 9 | LDOUBLE sum = 0.0; 10 | int counter = 0; 11 | for(double e : iter){ 12 | R_CHECK_USER_INTERRUPT(++counter); 13 | sum += e; 14 | } 15 | return sum; 16 | } 17 | 18 | 19 | 20 | // This function was copied from https://stackoverflow.com/a/17299623/604854 21 | template 22 | std::vector flatten(const std::vector>& v) { 23 | std::size_t total_size = 0; 24 | for (const auto& sub : v){ 25 | total_size += sub.size(); // I wish there was a transform_accumulate 26 | } 27 | std::vector result; 28 | result.reserve(total_size); 29 | for (const auto& sub : v) 30 | result.insert(result.end(), sub.begin(), sub.end()); 31 | return result; 32 | } 33 | 34 | 35 | template 36 | inline bool is_any_na(Iterator iter){ 37 | return std::any_of(iter.begin(), iter.end(), [](const double d) -> bool { 38 | return Rcpp::NumericVector::is_na(d); 39 | }); 40 | } 41 | 42 | 43 | template 44 | inline bool are_all_na(Iterator iter){ 45 | return std::all_of(iter.begin(), iter.end(), [](const double d) -> bool { 46 | return Rcpp::NumericVector::is_na(d); 47 | }); 48 | } 49 | 50 | 51 | 52 | #endif /* my_utils_h */ 53 | -------------------------------------------------------------------------------- /man/colCumsums-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colCumsums,xgCMatrix-method} 4 | \alias{colCumsums,xgCMatrix-method} 5 | \alias{rowCumsums,xgCMatrix-method} 6 | \title{Calculates the cumulative sum for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colCumsums}{xgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 9 | 10 | \S4method{rowCumsums}{xgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 20 | } 21 | \value{ 22 | Returns a \code{\link{numeric}} \code{\link{matrix}}with the same 23 | dimensions as \code{x}. 24 | } 25 | \description{ 26 | Calculates the cumulative sum for each row (column) of a matrix-like object. 27 | } 28 | \details{ 29 | The S4 methods for \code{x} of type \code{\link{matrix}}, 30 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 31 | \code{matrixStats::\link[matrixStats]{rowCumsums}} / 32 | \code{matrixStats::\link[matrixStats]{colCumsums}}. 33 | } 34 | \examples{ 35 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 36 | mat[2, 1] <- NA 37 | mat[3, 3] <- Inf 38 | mat[4, 1] <- 0 39 | 40 | print(mat) 41 | 42 | rowCumsums(mat) 43 | colCumsums(mat) 44 | } 45 | \seealso{ 46 | \itemize{ 47 | \item \code{matrixStats::\link[matrixStats]{rowCumsums}()} and 48 | \code{matrixStats::\link[matrixStats:rowCumsums]{colCumsums}()} which are 49 | used when the input is a \code{matrix} or \code{numeric} vector. 50 | \item \code{base::\link{cumsum}()}. 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /man/colCumprods-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colCumprods,xgCMatrix-method} 4 | \alias{colCumprods,xgCMatrix-method} 5 | \alias{rowCumprods,xgCMatrix-method} 6 | \title{Calculates the cumulative product for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colCumprods}{xgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 10 | 11 | \S4method{rowCumprods}{xgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 12 | } 13 | \arguments{ 14 | \item{x}{An NxK matrix-like object.} 15 | 16 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 17 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 18 | done.} 19 | 20 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 21 | } 22 | \value{ 23 | Returns a \code{\link{numeric}} \code{\link{matrix}}with the same 24 | dimensions as \code{x}. 25 | } 26 | \description{ 27 | Calculates the cumulative product for each row (column) of a matrix-like 28 | object. 29 | } 30 | \details{ 31 | The S4 methods for \code{x} of type \code{\link{matrix}}, 32 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 33 | \code{matrixStats::\link[matrixStats]{rowCumprods}} / 34 | \code{matrixStats::\link[matrixStats]{colCumprods}}. 35 | } 36 | \examples{ 37 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 38 | mat[2, 1] <- NA 39 | mat[3, 3] <- Inf 40 | mat[4, 1] <- 0 41 | 42 | print(mat) 43 | 44 | rowCumprods(mat) 45 | colCumprods(mat) 46 | } 47 | \seealso{ 48 | \itemize{ 49 | \item \code{matrixStats::\link[matrixStats:rowCumsums]{rowCumprods}()} and 50 | \code{matrixStats::\link[matrixStats:rowCumsums]{colCumprods}()} which 51 | are used when the input is a \code{matrix} or \code{numeric} vector. 52 | \item \code{base::\link{cumprod}()}. 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /man/colAnyNAs-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colAnyNAs,xgCMatrix-method} 4 | \alias{colAnyNAs,xgCMatrix-method} 5 | \alias{rowAnyNAs,xgCMatrix-method} 6 | \title{Check if any elements in a row (column) of a matrix-like object is missing} 7 | \usage{ 8 | \S4method{colAnyNAs}{xgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 9 | 10 | \S4method{rowAnyNAs}{xgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 20 | } 21 | \value{ 22 | Returns a \code{\link{logical}} \code{\link{vector}} of length N (K). 23 | } 24 | \description{ 25 | Check if any elements in a row (column) of a matrix-like object is missing. 26 | } 27 | \details{ 28 | The S4 methods for \code{x} of type \code{\link{matrix}}, 29 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 30 | \code{matrixStats::\link[matrixStats]{rowAnyNAs}} / 31 | \code{matrixStats::\link[matrixStats]{colAnyNAs}}. 32 | } 33 | \examples{ 34 | mat <- matrix(0, nrow=10, ncol=5) 35 | mat[sample(seq_len(5 *10), 5)] <- NA 36 | sp_mat <- as(mat, "dgCMatrix") 37 | colAnyNAs(sp_mat) 38 | 39 | } 40 | \seealso{ 41 | \itemize{ 42 | \item \code{matrixStats::\link[matrixStats:anyMissing]{rowAnyNAs}()} and 43 | \code{matrixStats::\link[matrixStats:anyMissing]{colAnyNAs}()} which are 44 | used when the input is a \code{matrix} or \code{numeric} vector. 45 | \item For checks if any element is equal to a value, see 46 | \code{\link[MatrixGenerics]{rowAnys}()}. 47 | \item \code{base::\link{is.na}()} and \code{base::\link{any}()}. 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /man/colCummaxs-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colCummaxs,dgCMatrix-method} 4 | \alias{colCummaxs,dgCMatrix-method} 5 | \alias{rowCummaxs,dgCMatrix-method} 6 | \title{Calculates the cumulative maxima for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colCummaxs}{dgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 10 | 11 | \S4method{rowCummaxs}{dgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 12 | } 13 | \arguments{ 14 | \item{x}{An NxK matrix-like object.} 15 | 16 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 17 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 18 | done.} 19 | 20 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 21 | } 22 | \value{ 23 | Returns a \code{\link{numeric}} \code{\link{matrix}}with the same 24 | dimensions as \code{x}. 25 | } 26 | \description{ 27 | Calculates the cumulative maxima for each row (column) of a matrix-like 28 | object. 29 | } 30 | \details{ 31 | The S4 methods for \code{x} of type \code{\link{matrix}}, 32 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 33 | \code{matrixStats::\link[matrixStats]{rowCummaxs}} / 34 | \code{matrixStats::\link[matrixStats]{colCummaxs}}. 35 | } 36 | \examples{ 37 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 38 | mat[2, 1] <- NA 39 | mat[3, 3] <- Inf 40 | mat[4, 1] <- 0 41 | 42 | print(mat) 43 | 44 | rowCummaxs(mat) 45 | colCummaxs(mat) 46 | } 47 | \seealso{ 48 | \itemize{ 49 | \item \code{matrixStats::\link[matrixStats:rowCumsums]{rowCummaxs}()} and 50 | \code{matrixStats::\link[matrixStats:rowCumsums]{colCummaxs}()} which are 51 | used when the input is a \code{matrix} or \code{numeric} vector. 52 | \item For single maximum estimates, see \code{\link[MatrixGenerics]{rowMaxs}()}. 53 | \item \code{base::\link{cummax}()}. 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /man/colCummins-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colCummins,dgCMatrix-method} 4 | \alias{colCummins,dgCMatrix-method} 5 | \alias{rowCummins,dgCMatrix-method} 6 | \title{Calculates the cumulative minima for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colCummins}{dgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 10 | 11 | \S4method{rowCummins}{dgCMatrix}(x, rows = NULL, cols = NULL, useNames = TRUE) 12 | } 13 | \arguments{ 14 | \item{x}{An NxK matrix-like object.} 15 | 16 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 17 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 18 | done.} 19 | 20 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 21 | } 22 | \value{ 23 | Returns a \code{\link{numeric}} \code{\link{matrix}}with the same 24 | dimensions as \code{x}. 25 | } 26 | \description{ 27 | Calculates the cumulative minima for each row (column) of a matrix-like 28 | object. 29 | } 30 | \details{ 31 | The S4 methods for \code{x} of type \code{\link{matrix}}, 32 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 33 | \code{matrixStats::\link[matrixStats]{rowCummins}} / 34 | \code{matrixStats::\link[matrixStats]{colCummins}}. 35 | } 36 | \examples{ 37 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 38 | mat[2, 1] <- NA 39 | mat[3, 3] <- Inf 40 | mat[4, 1] <- 0 41 | 42 | print(mat) 43 | 44 | rowCummins(mat) 45 | colCummins(mat) 46 | } 47 | \seealso{ 48 | \itemize{ 49 | \item \code{matrixStats::\link[matrixStats:rowCumsums]{rowCummins}()} and 50 | \code{matrixStats::\link[matrixStats:rowCumsums]{colCummins}()} which are 51 | used when the input is a \code{matrix} or \code{numeric} vector. 52 | \item For single minimum estimates, see \code{\link[MatrixGenerics]{rowMins}()}. 53 | \item \code{base::\link{cummin}()}. 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /man/colCollapse-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colCollapse,xgCMatrix-method} 4 | \alias{colCollapse,xgCMatrix-method} 5 | \alias{rowCollapse,xgCMatrix-method} 6 | \title{Extract one cell from each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colCollapse}{xgCMatrix}(x, idxs, cols = NULL, useNames = TRUE) 9 | 10 | \S4method{rowCollapse}{xgCMatrix}(x, idxs, rows = NULL, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{idxs}{An index \code{\link{vector}} with the position to extract. 16 | It is recycled to match the number of rows (column)} 17 | 18 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 19 | 20 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 21 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 22 | done.} 23 | } 24 | \value{ 25 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 26 | } 27 | \description{ 28 | Extract one cell from each row (column) of a matrix-like object. 29 | } 30 | \details{ 31 | The S4 methods for \code{x} of type \code{\link{matrix}}, 32 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 33 | \code{matrixStats::\link[matrixStats]{rowCollapse}} / 34 | \code{matrixStats::\link[matrixStats]{colCollapse}}. 35 | } 36 | \examples{ 37 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 38 | mat[2, 1] <- NA 39 | mat[3, 3] <- Inf 40 | mat[4, 1] <- 0 41 | 42 | print(mat) 43 | 44 | rowCollapse(mat, idxs = 2) 45 | rowCollapse(mat, idxs = c(1,1,2,3,2)) 46 | 47 | colCollapse (mat, idxs = 4) 48 | } 49 | \seealso{ 50 | \itemize{ 51 | \item \code{matrixStats::\link[matrixStats:rowCollapse]{rowCollapse}()} 52 | and \code{matrixStats::\link[matrixStats:rowCollapse]{colCollapse}()} 53 | which are used when the input is a \code{matrix} or \code{numeric} vector. 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /man/colMaxs-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colMaxs,dgCMatrix-method} 4 | \alias{colMaxs,dgCMatrix-method} 5 | \alias{rowMaxs,dgCMatrix-method} 6 | \title{Calculates the maximum for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colMaxs}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 9 | 10 | \S4method{rowMaxs}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 20 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 21 | omitted from the calculations.} 22 | 23 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 24 | } 25 | \value{ 26 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 27 | } 28 | \description{ 29 | Calculates the maximum for each row (column) of a matrix-like object. 30 | } 31 | \details{ 32 | The S4 methods for \code{x} of type \code{\link{matrix}}, 33 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 34 | \code{matrixStats::\link[matrixStats]{rowMaxs}} / 35 | \code{matrixStats::\link[matrixStats]{colMaxs}}. 36 | } 37 | \examples{ 38 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 39 | mat[2, 1] <- NA 40 | mat[3, 3] <- Inf 41 | mat[4, 1] <- 0 42 | 43 | print(mat) 44 | 45 | rowMaxs(mat) 46 | colMaxs(mat) 47 | } 48 | \seealso{ 49 | \itemize{ 50 | \item \code{matrixStats::\link[matrixStats:rowRanges]{rowMaxs}()} and 51 | \code{matrixStats::\link[matrixStats:rowRanges]{colMaxs}()} which are used 52 | when the input is a \code{matrix} or \code{numeric} vector. 53 | \item For min estimates, see \code{\link[MatrixGenerics]{rowMins}()}. 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /man/colMins-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colMins,dgCMatrix-method} 4 | \alias{colMins,dgCMatrix-method} 5 | \alias{rowMins,dgCMatrix-method} 6 | \title{Calculates the minimum for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colMins}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 9 | 10 | \S4method{rowMins}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 20 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 21 | omitted from the calculations.} 22 | 23 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 24 | } 25 | \value{ 26 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 27 | } 28 | \description{ 29 | Calculates the minimum for each row (column) of a matrix-like object. 30 | } 31 | \details{ 32 | The S4 methods for \code{x} of type \code{\link{matrix}}, 33 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 34 | \code{matrixStats::\link[matrixStats]{rowMins}} / 35 | \code{matrixStats::\link[matrixStats]{colMins}}. 36 | } 37 | \examples{ 38 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 39 | mat[2, 1] <- NA 40 | mat[3, 3] <- Inf 41 | mat[4, 1] <- 0 42 | 43 | print(mat) 44 | 45 | rowMins(mat) 46 | colMins(mat) 47 | } 48 | \seealso{ 49 | \itemize{ 50 | \item \code{matrixStats::\link[matrixStats:rowRanges]{rowMins}()} and 51 | \code{matrixStats::\link[matrixStats:rowRanges]{colMins}()} which are used 52 | when the input is a \code{matrix} or \code{numeric} vector. 53 | \item For max estimates, see \code{\link[MatrixGenerics]{rowMaxs}()}. 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /man/colMedians-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colMedians,dgCMatrix-method} 4 | \alias{colMedians,dgCMatrix-method} 5 | \alias{rowMedians,dgCMatrix-method} 6 | \title{Calculates the median for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colMedians}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 9 | 10 | \S4method{rowMedians}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 20 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 21 | omitted from the calculations.} 22 | 23 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 24 | } 25 | \value{ 26 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 27 | } 28 | \description{ 29 | Calculates the median for each row (column) of a matrix-like object. 30 | } 31 | \details{ 32 | The S4 methods for \code{x} of type \code{\link{matrix}}, 33 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 34 | \code{matrixStats::\link[matrixStats]{rowMedians}} / 35 | \code{matrixStats::\link[matrixStats]{colMedians}}. 36 | } 37 | \examples{ 38 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 39 | mat[2, 1] <- NA 40 | mat[3, 3] <- Inf 41 | mat[4, 1] <- 0 42 | 43 | print(mat) 44 | 45 | rowMedians(mat) 46 | colMedians(mat) 47 | } 48 | \seealso{ 49 | \itemize{ 50 | \item \code{matrixStats::\link[matrixStats]{rowMedians}()} and 51 | \code{matrixStats::\link[matrixStats:rowMedians]{colMedians}()} which are 52 | used when the input is a \code{matrix} or \code{numeric} vector. 53 | \item For mean estimates, see \code{\link[MatrixGenerics]{rowMeans2}()} and 54 | \code{\link[base:colSums]{rowMeans}()}. 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /man/colSums2-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colSums2,xgCMatrix-method} 4 | \alias{colSums2,xgCMatrix-method} 5 | \alias{rowSums2,xgCMatrix-method} 6 | \title{Calculates the sum for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colSums2}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 9 | 10 | \S4method{rowSums2}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 20 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 21 | omitted from the calculations.} 22 | 23 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 24 | } 25 | \value{ 26 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 27 | } 28 | \description{ 29 | Calculates the sum for each row (column) of a matrix-like object. 30 | } 31 | \details{ 32 | The S4 methods for \code{x} of type \code{\link{matrix}}, 33 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 34 | \code{matrixStats::\link[matrixStats]{rowSums2}} / 35 | \code{matrixStats::\link[matrixStats]{colSums2}}. 36 | } 37 | \examples{ 38 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 39 | mat[2, 1] <- NA 40 | mat[3, 3] <- Inf 41 | mat[4, 1] <- 0 42 | 43 | print(mat) 44 | 45 | rowSums2(mat) 46 | colSums2(mat) 47 | } 48 | \seealso{ 49 | \itemize{ 50 | \item \code{matrixStats::\link[matrixStats]{rowSums2}()} and 51 | \code{matrixStats::\link[matrixStats:rowSums2]{colSums2}()} which are used 52 | when the input is a \code{matrix} or \code{numeric} vector. 53 | \item For mean estimates, see \code{\link[MatrixGenerics]{rowMeans2}()} and 54 | \code{\link[base:colSums]{rowMeans}()}. 55 | \item \code{base::\link{sum}()}. 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /man/colTabulates-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colTabulates,xgCMatrix-method} 4 | \alias{colTabulates,xgCMatrix-method} 5 | \alias{rowTabulates,xgCMatrix-method} 6 | \title{Tabulates the values in a matrix-like object by row (column)} 7 | \usage{ 8 | \S4method{colTabulates}{xgCMatrix}(x, rows = NULL, cols = NULL, values = NULL, useNames = TRUE) 9 | 10 | \S4method{rowTabulates}{xgCMatrix}(x, rows = NULL, cols = NULL, values = NULL, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{values}{the values to search for.} 20 | 21 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 22 | } 23 | \value{ 24 | a \code{\link{numeric}} \code{NxJ} (\code{KxJ}) 25 | \code{\link{matrix}}, where N (K) is the number of rows (columns) for 26 | which the J values are calculated. 27 | } 28 | \description{ 29 | Tabulates the values in a matrix-like object by row (column). 30 | } 31 | \details{ 32 | The S4 methods for \code{x} of type \code{\link{matrix}}, 33 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 34 | \code{matrixStats::\link[matrixStats]{rowTabulates}} / 35 | \code{matrixStats::\link[matrixStats]{colTabulates}}. 36 | } 37 | \examples{ 38 | mat <- matrix(rpois(15, lambda = 3), nrow = 5, ncol = 3) 39 | mat[2, 1] <- NA_integer_ 40 | mat[3, 3] <- 0L 41 | mat[4, 1] <- 0L 42 | 43 | print(mat) 44 | 45 | rowTabulates(mat) 46 | colTabulates(mat) 47 | 48 | rowTabulates(mat, values = 0) 49 | colTabulates(mat, values = 0) 50 | } 51 | \seealso{ 52 | \itemize{ 53 | \item \code{matrixStats::\link[matrixStats]{rowTabulates}()} and 54 | \code{matrixStats::\link[matrixStats:rowTabulates]{colTabulates}()} which 55 | are used when the input is a \code{matrix} or \code{numeric} vector. 56 | \item \code{base::\link{table}()} 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /man/colIQRs-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colIQRs,xgCMatrix-method} 4 | \alias{colIQRs,xgCMatrix-method} 5 | \alias{rowIQRs,xgCMatrix-method} 6 | \title{Calculates the interquartile range for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colIQRs}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 10 | 11 | \S4method{rowIQRs}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 12 | } 13 | \arguments{ 14 | \item{x}{An NxK matrix-like object.} 15 | 16 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 17 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 18 | done.} 19 | 20 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 21 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 22 | omitted from the calculations.} 23 | 24 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 25 | } 26 | \value{ 27 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 28 | } 29 | \description{ 30 | Calculates the interquartile range for each row (column) of a matrix-like 31 | object. 32 | } 33 | \details{ 34 | The S4 methods for \code{x} of type \code{\link{matrix}}, 35 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 36 | \code{matrixStats::\link[matrixStats]{rowIQRs}} / 37 | \code{matrixStats::\link[matrixStats]{colIQRs}}. 38 | } 39 | \examples{ 40 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 41 | mat[2, 1] <- NA 42 | mat[3, 3] <- Inf 43 | mat[4, 1] <- 0 44 | 45 | print(mat) 46 | 47 | rowIQRs(mat) 48 | colIQRs(mat) 49 | } 50 | \seealso{ 51 | \itemize{ 52 | \item \code{matrixStats::\link[matrixStats]{rowIQRs}()} and 53 | \code{matrixStats::\link[matrixStats:rowIQRs]{colIQRs}()} which are used 54 | when the input is a \code{matrix} or \code{numeric} vector. 55 | \item For a non-robust analog, see \code{\link[MatrixGenerics]{rowSds}()}. For a more 56 | robust version see \code{\link[MatrixGenerics:rowMads]{rowMads()}} 57 | \item \code{stats::\link[stats]{IQR}()}. 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /man/colDiffs-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colDiffs,dgCMatrix-method} 4 | \alias{colDiffs,dgCMatrix-method} 5 | \alias{rowDiffs,dgCMatrix-method} 6 | \title{Calculates the difference between each element of a row (column) of a 7 | matrix-like object} 8 | \usage{ 9 | \S4method{colDiffs}{dgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | lag = 1L, 14 | differences = 1L, 15 | useNames = TRUE 16 | ) 17 | 18 | \S4method{rowDiffs}{dgCMatrix}( 19 | x, 20 | rows = NULL, 21 | cols = NULL, 22 | lag = 1L, 23 | differences = 1L, 24 | useNames = TRUE 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An NxK matrix-like object.} 29 | 30 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 31 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 32 | done.} 33 | 34 | \item{lag}{An integer specifying the lag.} 35 | 36 | \item{differences}{An integer specifying the order of difference.} 37 | 38 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 39 | } 40 | \value{ 41 | Returns a \code{\link{numeric}} \code{\link{matrix}} with one column 42 | (row) less than x: \eqn{Nx(K-1)} or \eqn{(N-1)xK}. 43 | } 44 | \description{ 45 | Calculates the difference between each element of a row (column) of a 46 | matrix-like object. 47 | } 48 | \details{ 49 | The S4 methods for \code{x} of type \code{\link{matrix}}, 50 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 51 | \code{matrixStats::\link[matrixStats]{rowDiffs}} / 52 | \code{matrixStats::\link[matrixStats]{colDiffs}}. 53 | } 54 | \examples{ 55 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 56 | mat[2, 1] <- NA 57 | mat[3, 3] <- Inf 58 | mat[4, 1] <- 0 59 | 60 | print(mat) 61 | 62 | rowDiffs(mat) 63 | colDiffs(mat) 64 | } 65 | \seealso{ 66 | \itemize{ 67 | \item \code{matrixStats::\link[matrixStats]{rowDiffs}()} and 68 | \code{matrixStats::\link[matrixStats:rowDiffs]{colDiffs}()} which are used 69 | when the input is a \code{matrix} or \code{numeric} vector. 70 | \item \code{base::\link{diff}()}. 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /man/colLogSumExps-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colLogSumExps,xgCMatrix-method} 4 | \alias{colLogSumExps,xgCMatrix-method} 5 | \alias{rowLogSumExps,xgCMatrix-method} 6 | \title{Accurately calculates the logarithm of the sum of exponentials for each row 7 | (column) of a matrix-like object} 8 | \usage{ 9 | \S4method{colLogSumExps}{xgCMatrix}(lx, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 10 | 11 | \S4method{rowLogSumExps}{xgCMatrix}(lx, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 12 | } 13 | \arguments{ 14 | \item{lx}{An NxK matrix-like object. Typically \code{lx} are \code{log(x)} values.} 15 | 16 | \item{rows, cols}{A \code{\link{vector}} indicating the subset (and/or 17 | columns) to operate over. If \code{\link{NULL}}, no subsetting is done.} 18 | 19 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 20 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 21 | omitted from the calculations.} 22 | 23 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 24 | } 25 | \value{ 26 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 27 | } 28 | \description{ 29 | Accurately calculates the logarithm of the sum of exponentials for each row 30 | (column) of a matrix-like object. 31 | } 32 | \details{ 33 | The S4 methods for \code{x} of type \code{\link{matrix}}, 34 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 35 | \code{matrixStats::\link[matrixStats]{rowLogSumExps}} / 36 | \code{matrixStats::\link[matrixStats]{colLogSumExps}}. 37 | } 38 | \examples{ 39 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 40 | mat[2, 1] <- NA 41 | mat[3, 3] <- Inf 42 | mat[4, 1] <- 0 43 | 44 | print(mat) 45 | 46 | rowLogSumExps(mat) 47 | colLogSumExps(mat) 48 | } 49 | \seealso{ 50 | \itemize{ 51 | \item \code{matrixStats::\link[matrixStats]{rowLogSumExps}()} and 52 | \code{matrixStats::\link[matrixStats:rowLogSumExps]{colLogSumExps}()} 53 | which are used when the input is a \code{matrix} or \code{numeric} vector. 54 | \item \code{\link[MatrixGenerics:rowSums2]{rowSums2()}} 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /man/colOrderStats-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colOrderStats,dgCMatrix-method} 4 | \alias{colOrderStats,dgCMatrix-method} 5 | \alias{rowOrderStats,dgCMatrix-method} 6 | \title{Calculates an order statistic for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colOrderStats}{dgCMatrix}( 9 | x, 10 | rows = NULL, 11 | cols = NULL, 12 | which = 1, 13 | na.rm = FALSE, 14 | useNames = TRUE 15 | ) 16 | 17 | \S4method{rowOrderStats}{dgCMatrix}( 18 | x, 19 | rows = NULL, 20 | cols = NULL, 21 | which = 1, 22 | na.rm = FALSE, 23 | useNames = TRUE 24 | ) 25 | } 26 | \arguments{ 27 | \item{x}{An NxK matrix-like object.} 28 | 29 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 30 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 31 | done.} 32 | 33 | \item{which}{An integer index in [1,K] ([1,N]) indicating which order 34 | statistic to be returned} 35 | 36 | \item{na.rm}{If TRUE, NAs are excluded first, otherwise not.} 37 | 38 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 39 | } 40 | \value{ 41 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 42 | } 43 | \description{ 44 | Calculates an order statistic for each row (column) of a matrix-like object. 45 | } 46 | \details{ 47 | The S4 methods for \code{x} of type \code{\link{matrix}}, 48 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 49 | \code{matrixStats::\link[matrixStats]{rowOrderStats}} / 50 | \code{matrixStats::\link[matrixStats]{colOrderStats}}. 51 | } 52 | \examples{ 53 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 54 | mat[2, 1] <- 2 55 | mat[3, 3] <- Inf 56 | mat[4, 1] <- 0 57 | 58 | print(mat) 59 | 60 | rowOrderStats(mat, which = 1) 61 | colOrderStats(mat, which = 3) 62 | } 63 | \seealso{ 64 | \itemize{ 65 | \item \code{matrixStats::\link[matrixStats]{rowOrderStats}()} and 66 | \code{matrixStats::\link[matrixStats:rowOrderStats]{colOrderStats}()} 67 | which are used when the input is a \code{matrix} or \code{numeric} vector. 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /man/colMeans2-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colMeans2,xgCMatrix-method} 4 | \alias{colMeans2,xgCMatrix-method} 5 | \alias{rowMeans2,xgCMatrix-method} 6 | \title{Calculates the mean for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colMeans2}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 9 | 10 | \S4method{rowMeans2}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 20 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 21 | omitted from the calculations.} 22 | 23 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 24 | } 25 | \value{ 26 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 27 | } 28 | \description{ 29 | Calculates the mean for each row (column) of a matrix-like object. 30 | } 31 | \details{ 32 | The S4 methods for \code{x} of type \code{\link{matrix}}, 33 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 34 | \code{matrixStats::\link[matrixStats]{rowMeans2}} / 35 | \code{matrixStats::\link[matrixStats]{colMeans2}}. 36 | } 37 | \examples{ 38 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 39 | mat[2, 1] <- NA 40 | mat[3, 3] <- Inf 41 | mat[4, 1] <- 0 42 | 43 | print(mat) 44 | 45 | rowMeans2(mat) 46 | colMeans2(mat) 47 | } 48 | \seealso{ 49 | \itemize{ 50 | \item \code{matrixStats::\link[matrixStats]{rowMeans2}()} and 51 | \code{matrixStats::\link[matrixStats:rowMeans2]{colMeans2}()} which are 52 | used when the input is a \code{matrix} or \code{numeric} vector. 53 | \item See also \code{\link[base:colSums]{rowMeans}()} for the 54 | corresponding function in base R. 55 | \item For variance estimates, see \code{\link[MatrixGenerics]{rowVars}()}. 56 | \item See also the base R version \code{base::\link[MatrixGenerics]{rowMeans}()}. 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /man/colProds-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colProds,xgCMatrix-method} 4 | \alias{colProds,xgCMatrix-method} 5 | \alias{rowProds,xgCMatrix-method} 6 | \title{Calculates the product for each row (column) in a matrix} 7 | \usage{ 8 | \S4method{colProds}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 9 | 10 | \S4method{rowProds}{xgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 11 | } 12 | \arguments{ 13 | \item{x}{An NxK matrix-like object.} 14 | 15 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 16 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 17 | done.} 18 | 19 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 20 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 21 | omitted from the calculations.} 22 | 23 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 24 | } 25 | \value{ 26 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 27 | } 28 | \description{ 29 | Calculates the product for each row (column) in a matrix 30 | } 31 | \details{ 32 | Attention: This method ignores the order of the values, because it assumes that 33 | the product is commutative. Unfortunately, for 'double' this is not true. 34 | For example `NaN * NA = NaN`, but `NA * NaN = NA`. This is relevant for this 35 | function if there are `+-Inf`, because `Inf * 0 = NaN`. This function returns 36 | `NA` whenever there is `NA` in the input. This is different from `matrixStats::colProds()`. 37 | } 38 | \examples{ 39 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 40 | mat[2, 1] <- NA 41 | mat[3, 3] <- Inf 42 | mat[4, 1] <- 0 43 | 44 | print(mat) 45 | 46 | rowProds(mat) 47 | colProds(mat) 48 | } 49 | \seealso{ 50 | \itemize{ 51 | \item \code{matrixStats::\link[matrixStats]{rowProds}()} and 52 | \code{matrixStats::\link[matrixStats:rowProds]{colProds}()} which are used 53 | when the input is a \code{matrix} or \code{numeric} vector. 54 | \item For sums across rows (columns), see 55 | \code{rowSums2()} (\code{colSums2()}) 56 | \item \code{base::\link{prod}()}. 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/ColumnView.h: -------------------------------------------------------------------------------- 1 | #ifndef ColumnView_h 2 | #define ColumnView_h 3 | 4 | #include 5 | #include "SparseMatrixView.h" 6 | #include "VectorSubsetView.h" 7 | 8 | 9 | class ColumnView { 10 | const dgCMatrixView* matrix; 11 | 12 | public: 13 | class col_container { 14 | public: 15 | VectorSubsetView values; 16 | VectorSubsetView row_indices; 17 | const R_len_t number_of_zeros; 18 | 19 | col_container(VectorSubsetView values_, VectorSubsetView row_indices_, R_len_t number_of_zeros_): 20 | values(values_), row_indices(row_indices_), number_of_zeros(number_of_zeros_) {} 21 | }; 22 | 23 | 24 | class iterator { 25 | ColumnView* cv; 26 | int index; 27 | public: 28 | using iterator_category= std::input_iterator_tag; 29 | using value_type = col_container; 30 | using reference = col_container&; 31 | using pointer = col_container*; 32 | using difference_type = void; 33 | 34 | iterator(ColumnView* cv_): cv(cv_), index(0) { 35 | if(cv != nullptr && cv->matrix->ncol == 0){ 36 | cv = nullptr; 37 | } 38 | } 39 | 40 | col_container operator*() const { 41 | int start_pos = cv->matrix->col_ptrs[index]; 42 | int end_pos = cv->matrix->col_ptrs[index + 1]; 43 | int number_of_zeros = cv->matrix->nrow - (end_pos - start_pos); 44 | VectorSubsetView values(cv->matrix->values, start_pos, end_pos); 45 | VectorSubsetView row_indices(cv->matrix->row_indices, start_pos, end_pos); 46 | 47 | return col_container(values, row_indices, number_of_zeros); 48 | } 49 | 50 | iterator& operator++(){ // preincrement 51 | ++index; 52 | if(index == cv->matrix->ncol) 53 | cv=nullptr; 54 | return *this; 55 | } 56 | 57 | friend bool operator==(iterator const& lhs,iterator const& rhs){ 58 | return lhs.cv == rhs.cv; 59 | } 60 | friend bool operator!=(iterator const& lhs,iterator const& rhs){ 61 | return !(lhs==rhs); 62 | } 63 | 64 | }; 65 | 66 | ColumnView(dgCMatrixView* matrix_): matrix(matrix_) {} 67 | iterator begin() { return iterator(this); } 68 | iterator end() { return iterator(nullptr); } 69 | 70 | }; 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | #endif /* ColumnView_h */ 83 | -------------------------------------------------------------------------------- /man/colAlls-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colAlls,xgCMatrix-method} 4 | \alias{colAlls,xgCMatrix-method} 5 | \alias{rowAlls,xgCMatrix-method} 6 | \title{Check if all elements in a row (column) of a matrix-like object are equal to 7 | a value} 8 | \usage{ 9 | \S4method{colAlls}{xgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | value = TRUE, 14 | na.rm = FALSE, 15 | useNames = TRUE 16 | ) 17 | 18 | \S4method{rowAlls}{xgCMatrix}( 19 | x, 20 | rows = NULL, 21 | cols = NULL, 22 | value = TRUE, 23 | na.rm = FALSE, 24 | useNames = TRUE 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An NxK matrix-like object.} 29 | 30 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 31 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 32 | done.} 33 | 34 | \item{value}{The value to search for.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 41 | } 42 | \value{ 43 | Returns a \code{\link{logical}} \code{\link{vector}} of length N (K). 44 | } 45 | \description{ 46 | Check if all elements in a row (column) of a matrix-like object are equal to 47 | a value. 48 | } 49 | \details{ 50 | The S4 methods for \code{x} of type \code{\link{matrix}}, 51 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 52 | \code{matrixStats::\link[matrixStats]{rowAlls}} / 53 | \code{matrixStats::\link[matrixStats]{colAlls}}. 54 | } 55 | \examples{ 56 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 57 | mat[2, 1] <- NA 58 | mat[3, 3] <- Inf 59 | mat[4, 1] <- 0 60 | 61 | print(mat) 62 | 63 | rowAlls(mat) 64 | colAlls(mat) 65 | } 66 | \seealso{ 67 | \itemize{ 68 | \item \code{matrixStats::\link[matrixStats:rowAlls]{rowAlls}()} and 69 | \code{matrixStats::\link[matrixStats:rowAlls]{colAlls}()} which are 70 | used when the input is a \code{matrix} or \code{numeric} vector. 71 | \item For checks if \emph{any} element is equal to a value, see 72 | \code{\link[MatrixGenerics]{rowAnys}()}. 73 | \item \code{base::\link{all}()}. 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /man/colAnys-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colAnys,xgCMatrix-method} 4 | \alias{colAnys,xgCMatrix-method} 5 | \alias{rowAnys,xgCMatrix-method} 6 | \title{Check if any elements in a row (column) of a matrix-like object is equal to 7 | a value} 8 | \usage{ 9 | \S4method{colAnys}{xgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | value = TRUE, 14 | na.rm = FALSE, 15 | useNames = TRUE 16 | ) 17 | 18 | \S4method{rowAnys}{xgCMatrix}( 19 | x, 20 | rows = NULL, 21 | cols = NULL, 22 | value = TRUE, 23 | na.rm = FALSE, 24 | useNames = TRUE 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An NxK matrix-like object.} 29 | 30 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 31 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 32 | done.} 33 | 34 | \item{value}{The value to search for.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 41 | } 42 | \value{ 43 | Returns a \code{\link{logical}} \code{\link{vector}} of length N (K). 44 | } 45 | \description{ 46 | Check if any elements in a row (column) of a matrix-like object is equal to 47 | a value. 48 | } 49 | \details{ 50 | The S4 methods for \code{x} of type \code{\link{matrix}}, 51 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 52 | \code{matrixStats::\link[matrixStats]{rowAnys}} / 53 | \code{matrixStats::\link[matrixStats]{colAnys}}. 54 | } 55 | \examples{ 56 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 57 | mat[2, 1] <- NA 58 | mat[3, 3] <- Inf 59 | mat[4, 1] <- 0 60 | 61 | print(mat) 62 | 63 | rowAnys(mat) 64 | colAnys(mat) 65 | } 66 | \seealso{ 67 | \itemize{ 68 | \item \code{matrixStats::\link[matrixStats:rowAlls]{rowAnys}()} and 69 | \code{matrixStats::\link[matrixStats:rowAlls]{colAnys}()} which are 70 | used when the input is a \code{matrix} or \code{numeric} vector. 71 | \item For checks if \emph{all} elements are equal to a value, see 72 | \code{\link[MatrixGenerics]{rowAlls}()}. 73 | \item \code{base::\link{any}()}. 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /man/colSds-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colSds,xgCMatrix-method} 4 | \alias{colSds,xgCMatrix-method} 5 | \alias{rowSds,xgCMatrix-method} 6 | \title{Calculates the standard deviation for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colSds}{xgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | na.rm = FALSE, 14 | center = NULL, 15 | useNames = TRUE 16 | ) 17 | 18 | \S4method{rowSds}{xgCMatrix}( 19 | x, 20 | rows = NULL, 21 | cols = NULL, 22 | na.rm = FALSE, 23 | center = NULL, 24 | useNames = TRUE 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An NxK matrix-like object.} 29 | 30 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 31 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 32 | done.} 33 | 34 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 35 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 36 | omitted from the calculations.} 37 | 38 | \item{center}{(optional) the center, defaults to the row means} 39 | 40 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 41 | } 42 | \value{ 43 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 44 | } 45 | \description{ 46 | Calculates the standard deviation for each row (column) of a matrix-like 47 | object. 48 | } 49 | \details{ 50 | The S4 methods for \code{x} of type \code{\link{matrix}}, 51 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 52 | \code{matrixStats::\link[matrixStats]{rowSds}} / 53 | \code{matrixStats::\link[matrixStats]{colSds}}. 54 | } 55 | \examples{ 56 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 57 | mat[2, 1] <- NA 58 | mat[3, 3] <- Inf 59 | mat[4, 1] <- 0 60 | 61 | print(mat) 62 | 63 | rowSds(mat) 64 | colSds(mat) 65 | } 66 | \seealso{ 67 | \itemize{ 68 | \item \code{matrixStats::\link[matrixStats]{rowSds}()} and 69 | \code{matrixStats::\link[matrixStats:rowSds]{colSds}()} which are used when 70 | the input is a \code{matrix} or \code{numeric} vector. 71 | \item For mean estimates, see \code{\link[MatrixGenerics]{rowMeans2}()} and 72 | \code{\link[base:colSums]{rowMeans}()}. 73 | \item For variance estimates, see \code{\link[MatrixGenerics]{rowVars}()}. 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /man/colVars-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colVars,xgCMatrix-method} 4 | \alias{colVars,xgCMatrix-method} 5 | \alias{rowVars,xgCMatrix-method} 6 | \title{Calculates the variance for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colVars}{xgCMatrix}( 9 | x, 10 | rows = NULL, 11 | cols = NULL, 12 | na.rm = FALSE, 13 | center = NULL, 14 | useNames = TRUE 15 | ) 16 | 17 | \S4method{rowVars}{xgCMatrix}( 18 | x, 19 | rows = NULL, 20 | cols = NULL, 21 | na.rm = FALSE, 22 | center = NULL, 23 | useNames = TRUE 24 | ) 25 | } 26 | \arguments{ 27 | \item{x}{An NxK matrix-like object.} 28 | 29 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 30 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 31 | done.} 32 | 33 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 34 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 35 | omitted from the calculations.} 36 | 37 | \item{center}{(optional) the center, defaults to the row means.} 38 | 39 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 40 | } 41 | \value{ 42 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 43 | } 44 | \description{ 45 | Calculates the variance for each row (column) of a matrix-like object. 46 | } 47 | \details{ 48 | The S4 methods for \code{x} of type \code{\link{matrix}}, 49 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 50 | \code{matrixStats::\link[matrixStats]{rowVars}} / 51 | \code{matrixStats::\link[matrixStats]{colVars}}. 52 | } 53 | \examples{ 54 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 55 | mat[2, 1] <- NA 56 | mat[3, 3] <- Inf 57 | mat[4, 1] <- 0 58 | 59 | print(mat) 60 | 61 | rowVars(mat) 62 | colVars(mat) 63 | } 64 | \seealso{ 65 | \itemize{ 66 | \item \code{matrixStats::\link[matrixStats]{rowVars}()} and 67 | \code{matrixStats::\link[matrixStats:rowVars]{colVars}()} which are used 68 | when the input is a \code{matrix} or \code{numeric} vector. 69 | \item For mean estimates, see \code{\link[MatrixGenerics]{rowMeans2}()} and 70 | \code{\link[base:colSums]{rowMeans}()}. 71 | \item For standard deviation estimates, see \code{\link[MatrixGenerics]{rowSds}()}. 72 | \item \code{stats::\link[stats:cor]{var}()}. 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /R/utils.R: -------------------------------------------------------------------------------- 1 | 2 | #============== Name setting functions ==============# 3 | 4 | # set the names attribute of res depending on useNames. If `useNames = NA`, use the value from `default`. 5 | # This method is specifically designed for the colXXX methods were the main argument is 6 | # usually called `x`. Instead of explicitly passing this information, I use a bit of reflection 7 | # to get the colnames of x 8 | set_result_names <- function(res, useNames, default = FALSE, names = colnames(parent.frame()$x)){ 9 | if(is.na(useNames)){ 10 | useNames <- default 11 | } 12 | if (useNames) { 13 | if (!is.null(names)) { 14 | # Zero-length attribute? Keep behavior same as base R function 15 | if (length(names) == 0L) names <- NULL 16 | names(res) <- names 17 | } 18 | } else { 19 | names(res) <- NULL 20 | } 21 | res 22 | } 23 | 24 | # same as `set_result_names()` but set the rownames of res 25 | set_result_rownames <- function(res, useNames, default = FALSE, names = colnames(parent.frame()$x)){ 26 | if(is.na(useNames)){ 27 | useNames <- default 28 | } 29 | if (useNames) { 30 | if (!is.null(names)) { 31 | # Zero-length attribute? Keep behavior same as base R function 32 | if (length(names) == 0L) names <- NULL 33 | rownames(res) <- names 34 | } 35 | } else { 36 | rownames(res) <- NULL 37 | } 38 | res 39 | } 40 | 41 | # same as `set_result_names()` but set the colnames of res 42 | set_result_colnames <- function(res, useNames, default = FALSE, names = colnames(parent.frame()$x)){ 43 | if(is.na(useNames)){ 44 | useNames <- default 45 | } 46 | if (useNames) { 47 | if (!is.null(names)) { 48 | # Zero-length attribute? Keep behavior same as base R function 49 | if (length(names) == 0L) names <- NULL 50 | colnames(res) <- names 51 | } 52 | } else { 53 | colnames(res) <- NULL 54 | } 55 | res 56 | } 57 | 58 | 59 | 60 | 61 | # same as `set_result_names()` but use names = rownames(x) as default 62 | set_result_names_t <- function(res, useNames, default = FALSE, names = rownames(parent.frame()$x)){ 63 | if(is.na(useNames)){ 64 | useNames <- default 65 | } 66 | if (useNames) { 67 | if (!is.null(names)) { 68 | # Zero-length attribute? Keep behavior same as base R function 69 | if (length(names) == 0L) names <- NULL 70 | names(res) <- names 71 | } 72 | } else { 73 | names(res) <- NULL 74 | } 75 | res 76 | } 77 | 78 | 79 | -------------------------------------------------------------------------------- /R/sparse_matrix_iterator.R: -------------------------------------------------------------------------------- 1 | 2 | reduce_sparse_matrix_to_num <- function(sp_mat, reduce_function = function(values, row_indices, number_of_zeros){ NA_real_}){ 3 | if(length(sp_mat@p) == 0){ 4 | numeric(0) 5 | }else{ 6 | vapply(seq_len(length(sp_mat@p)-1), function(index){ 7 | start_pos <- sp_mat@p[index] 8 | end_pos <- sp_mat@p[index + 1] 9 | number_of_zeros <- nrow(sp_mat) - (end_pos - start_pos) 10 | values <- sp_mat@x[start_pos + seq_len(end_pos - start_pos)] 11 | row_indices <- sp_mat@i[start_pos + seq_len(end_pos - start_pos)] 12 | reduce_function(values, row_indices, number_of_zeros) 13 | }, FUN.VALUE = 0.0) 14 | } 15 | } 16 | 17 | 18 | reduce_sparse_matrix_to_matrix <- function(sp_mat, n_result_rows, reduce_function = function(values, row_indices, number_of_zeros){ NA_real_}){ 19 | if(length(sp_mat@p) == 0){ 20 | numeric(0) 21 | }else{ 22 | res <- vapply(seq_len(length(sp_mat@p)-1), function(index){ 23 | start_pos <- sp_mat@p[index] 24 | end_pos <- sp_mat@p[index + 1] 25 | number_of_zeros <- nrow(sp_mat) - (end_pos - start_pos) 26 | values <- sp_mat@x[start_pos + seq_len(end_pos - start_pos)] 27 | row_indices <- sp_mat@i[start_pos + seq_len(end_pos - start_pos)] 28 | reduce_function(values, row_indices, number_of_zeros) 29 | }, FUN.VALUE = rep(0.0, n_result_rows)) 30 | if(n_result_rows == 1){ 31 | matrix(res, nrow=1, ncol=length(res)) 32 | }else{ 33 | res 34 | } 35 | } 36 | } 37 | 38 | 39 | expand_and_reduce_sparse_matrix_to_matrix <- function(sp_mat, n_result_rows, reduce_function = function(dense_values){ NA_real_}){ 40 | if(length(sp_mat@p) == 0){ 41 | numeric(0) 42 | }else{ 43 | res <- vapply(seq_len(length(sp_mat@p)-1), function(index){ 44 | start_pos <- sp_mat@p[index] 45 | end_pos <- sp_mat@p[index + 1] 46 | number_of_zeros <- nrow(sp_mat) - (end_pos - start_pos) 47 | values <- sp_mat@x[start_pos + seq_len(end_pos - start_pos)] 48 | row_indices <- sp_mat@i[start_pos + seq_len(end_pos - start_pos)] 49 | dense_values <- rep(0, nrow(sp_mat)) 50 | storage.mode(dense_values) <- typeof(values) 51 | dense_values[row_indices + 1] <- values 52 | reduce_function(dense_values) 53 | }, FUN.VALUE = vector(mode = typeof(sp_mat@x), n_result_rows)) 54 | if(n_result_rows == 1){ 55 | matrix(res, nrow=1, ncol=length(res)) 56 | }else{ 57 | res 58 | } 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /man/colWeightedMeans-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colWeightedMeans,xgCMatrix-method} 4 | \alias{colWeightedMeans,xgCMatrix-method} 5 | \alias{rowWeightedMeans,xgCMatrix-method} 6 | \title{Calculates the weighted mean for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colWeightedMeans}{xgCMatrix}( 9 | x, 10 | w = NULL, 11 | rows = NULL, 12 | cols = NULL, 13 | na.rm = FALSE, 14 | useNames = TRUE 15 | ) 16 | 17 | \S4method{rowWeightedMeans}{xgCMatrix}( 18 | x, 19 | w = NULL, 20 | rows = NULL, 21 | cols = NULL, 22 | na.rm = FALSE, 23 | useNames = TRUE 24 | ) 25 | } 26 | \arguments{ 27 | \item{x}{An NxK matrix-like object.} 28 | 29 | \item{w}{A \code{\link{numeric}} vector of length K (N) that specifies by 30 | how much each element is weighted.} 31 | 32 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 33 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 34 | done.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 41 | } 42 | \value{ 43 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 44 | } 45 | \description{ 46 | Calculates the weighted mean for each row (column) of a matrix-like object. 47 | } 48 | \details{ 49 | The S4 methods for \code{x} of type \code{\link{matrix}}, 50 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 51 | \code{matrixStats::\link[matrixStats]{rowWeightedMeans}} / 52 | \code{matrixStats::\link[matrixStats]{colWeightedMeans}}. 53 | } 54 | \examples{ 55 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 56 | mat[2, 1] <- NA 57 | mat[3, 3] <- Inf 58 | mat[4, 1] <- 0 59 | 60 | print(mat) 61 | w <- rnorm(n = 5, mean = 3) 62 | rowWeightedMeans(mat, w = w[1:3]) 63 | colWeightedMeans(mat, w = w) 64 | } 65 | \seealso{ 66 | \itemize{ 67 | \item \code{matrixStats::\link[matrixStats]{rowWeightedMeans}()} and 68 | \code{matrixStats::\link[matrixStats:rowWeightedMeans]{colWeightedMeans}()} 69 | which are used when the input is a \code{matrix} or \code{numeric} vector. 70 | \item See also \link[MatrixGenerics]{rowMeans2} for the corresponding unweighted function. 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /man/colWeightedVars-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colWeightedVars,xgCMatrix-method} 4 | \alias{colWeightedVars,xgCMatrix-method} 5 | \alias{rowWeightedVars,xgCMatrix-method} 6 | \title{Calculates the weighted variance for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colWeightedVars}{xgCMatrix}( 10 | x, 11 | w = NULL, 12 | rows = NULL, 13 | cols = NULL, 14 | na.rm = FALSE, 15 | useNames = TRUE 16 | ) 17 | 18 | \S4method{rowWeightedVars}{xgCMatrix}( 19 | x, 20 | w = NULL, 21 | rows = NULL, 22 | cols = NULL, 23 | na.rm = FALSE, 24 | useNames = TRUE 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An NxK matrix-like object.} 29 | 30 | \item{w}{A \code{\link{numeric}} vector of length K (N) that specifies by 31 | how much each element is weighted.} 32 | 33 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 34 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 35 | done.} 36 | 37 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 38 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 39 | omitted from the calculations.} 40 | 41 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 42 | } 43 | \value{ 44 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 45 | } 46 | \description{ 47 | Calculates the weighted variance for each row (column) of a matrix-like 48 | object. 49 | } 50 | \details{ 51 | The S4 methods for \code{x} of type \code{\link{matrix}}, 52 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 53 | \code{matrixStats::\link[matrixStats]{rowWeightedVars}} / 54 | \code{matrixStats::\link[matrixStats]{colWeightedVars}}. 55 | } 56 | \examples{ 57 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 58 | mat[2, 1] <- NA 59 | mat[3, 3] <- Inf 60 | mat[4, 1] <- 0 61 | 62 | print(mat) 63 | w <- rnorm(n = 5, mean = 3) 64 | rowWeightedVars(mat, w = w[1:3]) 65 | colWeightedVars(mat, w = w) 66 | } 67 | \seealso{ 68 | \itemize{ 69 | \item \code{matrixStats::\link[matrixStats:weightedVar]{rowWeightedVars}()} and 70 | \code{matrixStats::\link[matrixStats:weightedVar]{colWeightedVars}()} 71 | which are used when the input is a \code{matrix} or \code{numeric} vector. 72 | \item See also \link[MatrixGenerics]{rowVars} for the corresponding unweighted function. 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /man/colCounts-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colCounts,xgCMatrix-method} 4 | \alias{colCounts,xgCMatrix-method} 5 | \alias{rowCounts,xgCMatrix-method} 6 | \title{Count how often an element in a row (column) of a matrix-like object is 7 | equal to a value} 8 | \usage{ 9 | \S4method{colCounts}{xgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | value = TRUE, 14 | na.rm = FALSE, 15 | useNames = TRUE 16 | ) 17 | 18 | \S4method{rowCounts}{xgCMatrix}( 19 | x, 20 | rows = NULL, 21 | cols = NULL, 22 | value = TRUE, 23 | na.rm = FALSE, 24 | useNames = TRUE 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An NxK matrix-like object.} 29 | 30 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 31 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 32 | done.} 33 | 34 | \item{value}{The value to search for.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 41 | } 42 | \value{ 43 | Returns a \code{\link{integer}} \code{\link{vector}} of length N (K). 44 | } 45 | \description{ 46 | Count how often an element in a row (column) of a matrix-like object is 47 | equal to a value. 48 | } 49 | \details{ 50 | The S4 methods for \code{x} of type \code{\link{matrix}}, 51 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 52 | \code{matrixStats::\link[matrixStats]{rowCounts}} / 53 | \code{matrixStats::\link[matrixStats]{colCounts}}. 54 | } 55 | \examples{ 56 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 57 | mat[2, 1] <- NA 58 | mat[3, 3] <- Inf 59 | mat[4, 1] <- 0 60 | 61 | print(mat) 62 | 63 | rowCounts(mat) 64 | colCounts(mat) 65 | rowCounts(mat, value = 0) 66 | colCounts(mat, value = Inf, na.rm = TRUE) 67 | } 68 | \seealso{ 69 | \itemize{ 70 | \item \code{matrixStats::\link[matrixStats]{rowCounts}()} and 71 | \code{matrixStats::\link[matrixStats:rowCounts]{colCounts}()} which are 72 | used when the input is a \code{matrix} or \code{numeric} vector. 73 | \item For checks if any element is equal to a value, see 74 | \code{\link[MatrixGenerics]{rowAnys}()}. To check if all elements are equal, see 75 | \code{\link[MatrixGenerics]{rowAlls}()}. 76 | } 77 | } 78 | -------------------------------------------------------------------------------- /man/colWeightedSds-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colWeightedSds,xgCMatrix-method} 4 | \alias{colWeightedSds,xgCMatrix-method} 5 | \alias{rowWeightedSds,xgCMatrix-method} 6 | \title{Calculates the weighted standard deviation for each row (column) of a 7 | matrix-like object} 8 | \usage{ 9 | \S4method{colWeightedSds}{xgCMatrix}( 10 | x, 11 | w = NULL, 12 | rows = NULL, 13 | cols = NULL, 14 | na.rm = FALSE, 15 | useNames = TRUE 16 | ) 17 | 18 | \S4method{rowWeightedSds}{xgCMatrix}( 19 | x, 20 | w = NULL, 21 | rows = NULL, 22 | cols = NULL, 23 | na.rm = FALSE, 24 | useNames = TRUE 25 | ) 26 | } 27 | \arguments{ 28 | \item{x}{An NxK matrix-like object.} 29 | 30 | \item{w}{A \code{\link{numeric}} vector of length K (N) that specifies by 31 | how much each element is weighted.} 32 | 33 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 34 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 35 | done.} 36 | 37 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 38 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 39 | omitted from the calculations.} 40 | 41 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 42 | } 43 | \value{ 44 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 45 | } 46 | \description{ 47 | Calculates the weighted standard deviation for each row (column) of a 48 | matrix-like object. 49 | } 50 | \details{ 51 | The S4 methods for \code{x} of type \code{\link{matrix}}, 52 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 53 | \code{matrixStats::\link[matrixStats]{rowWeightedSds}} / 54 | \code{matrixStats::\link[matrixStats]{colWeightedSds}}. 55 | } 56 | \examples{ 57 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 58 | mat[2, 1] <- NA 59 | mat[3, 3] <- Inf 60 | mat[4, 1] <- 0 61 | 62 | print(mat) 63 | w <- rnorm(n = 5, mean = 3) 64 | rowWeightedSds(mat, w = w[1:3]) 65 | colWeightedSds(mat, w = w) 66 | } 67 | \seealso{ 68 | \itemize{ 69 | \item \code{matrixStats::\link[matrixStats:weightedVar]{rowWeightedSds}()} and 70 | \code{matrixStats::\link[matrixStats:weightedVar]{colWeightedSds}()} 71 | which are used when the input is a \code{matrix} or \code{numeric} vector. 72 | \item See also \link[MatrixGenerics]{rowSds} for the corresponding unweighted function. 73 | } 74 | } 75 | -------------------------------------------------------------------------------- /man/colWeightedMedians-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colWeightedMedians,dgCMatrix-method} 4 | \alias{colWeightedMedians,dgCMatrix-method} 5 | \alias{rowWeightedMedians,dgCMatrix-method} 6 | \title{Calculates the weighted median for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colWeightedMedians}{dgCMatrix}( 9 | x, 10 | w = NULL, 11 | rows = NULL, 12 | cols = NULL, 13 | na.rm = FALSE, 14 | useNames = TRUE 15 | ) 16 | 17 | \S4method{rowWeightedMedians}{dgCMatrix}( 18 | x, 19 | w = NULL, 20 | rows = NULL, 21 | cols = NULL, 22 | na.rm = FALSE, 23 | useNames = TRUE 24 | ) 25 | } 26 | \arguments{ 27 | \item{x}{An NxK matrix-like object.} 28 | 29 | \item{w}{A \code{\link{numeric}} vector of length K (N) that specifies by 30 | how much each element is weighted.} 31 | 32 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 33 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 34 | done.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 41 | } 42 | \value{ 43 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 44 | } 45 | \description{ 46 | Calculates the weighted median for each row (column) of a matrix-like object. 47 | } 48 | \details{ 49 | The S4 methods for \code{x} of type \code{\link{matrix}}, 50 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 51 | \code{matrixStats::\link[matrixStats]{rowWeightedMedians}} / 52 | \code{matrixStats::\link[matrixStats]{colWeightedMedians}}. 53 | } 54 | \examples{ 55 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 56 | mat[2, 1] <- NA 57 | mat[3, 3] <- Inf 58 | mat[4, 1] <- 0 59 | 60 | print(mat) 61 | w <- rnorm(n = 5, mean = 3) 62 | rowWeightedMedians(mat, w = w[1:3]) 63 | colWeightedMedians(mat, w = w) 64 | } 65 | \seealso{ 66 | \itemize{ 67 | \item \code{matrixStats::\link[matrixStats]{rowWeightedMedians}()} and 68 | \code{matrixStats::\link[matrixStats:rowWeightedMedians]{colWeightedMedians}()} 69 | which are used when the input is a \code{matrix} or \code{numeric} vector. 70 | \item See also \link[MatrixGenerics]{rowMedians} for the corresponding unweighted function. 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /NAMESPACE: -------------------------------------------------------------------------------- 1 | # Generated by roxygen2: do not edit by hand 2 | 3 | exportMethods(colAlls) 4 | exportMethods(colAnyNAs) 5 | exportMethods(colAnys) 6 | exportMethods(colAvgsPerRowSet) 7 | exportMethods(colCollapse) 8 | exportMethods(colCounts) 9 | exportMethods(colCummaxs) 10 | exportMethods(colCummins) 11 | exportMethods(colCumprods) 12 | exportMethods(colCumsums) 13 | exportMethods(colDiffs) 14 | exportMethods(colIQRDiffs) 15 | exportMethods(colIQRs) 16 | exportMethods(colLogSumExps) 17 | exportMethods(colMadDiffs) 18 | exportMethods(colMads) 19 | exportMethods(colMaxs) 20 | exportMethods(colMeans2) 21 | exportMethods(colMedians) 22 | exportMethods(colMins) 23 | exportMethods(colOrderStats) 24 | exportMethods(colProds) 25 | exportMethods(colQuantiles) 26 | exportMethods(colRanges) 27 | exportMethods(colRanks) 28 | exportMethods(colSdDiffs) 29 | exportMethods(colSds) 30 | exportMethods(colSums2) 31 | exportMethods(colTabulates) 32 | exportMethods(colVarDiffs) 33 | exportMethods(colVars) 34 | exportMethods(colWeightedMads) 35 | exportMethods(colWeightedMeans) 36 | exportMethods(colWeightedMedians) 37 | exportMethods(colWeightedSds) 38 | exportMethods(colWeightedVars) 39 | exportMethods(rowAlls) 40 | exportMethods(rowAnyNAs) 41 | exportMethods(rowAnys) 42 | exportMethods(rowAvgsPerColSet) 43 | exportMethods(rowCollapse) 44 | exportMethods(rowCounts) 45 | exportMethods(rowCummaxs) 46 | exportMethods(rowCummins) 47 | exportMethods(rowCumprods) 48 | exportMethods(rowCumsums) 49 | exportMethods(rowDiffs) 50 | exportMethods(rowIQRDiffs) 51 | exportMethods(rowIQRs) 52 | exportMethods(rowLogSumExps) 53 | exportMethods(rowMadDiffs) 54 | exportMethods(rowMads) 55 | exportMethods(rowMaxs) 56 | exportMethods(rowMeans2) 57 | exportMethods(rowMedians) 58 | exportMethods(rowMins) 59 | exportMethods(rowOrderStats) 60 | exportMethods(rowProds) 61 | exportMethods(rowQuantiles) 62 | exportMethods(rowRanges) 63 | exportMethods(rowRanks) 64 | exportMethods(rowSdDiffs) 65 | exportMethods(rowSds) 66 | exportMethods(rowSums2) 67 | exportMethods(rowTabulates) 68 | exportMethods(rowVarDiffs) 69 | exportMethods(rowVars) 70 | exportMethods(rowWeightedMads) 71 | exportMethods(rowWeightedMeans) 72 | exportMethods(rowWeightedMedians) 73 | exportMethods(rowWeightedSds) 74 | exportMethods(rowWeightedVars) 75 | import(MatrixGenerics) 76 | import(methods) 77 | importClassesFrom(Matrix,dgCMatrix) 78 | importClassesFrom(Matrix,lgCMatrix) 79 | importFrom(Matrix,t) 80 | importFrom(Rcpp,sourceCpp) 81 | importFrom(matrixStats,allocArray) 82 | importFrom(stats,setNames) 83 | useDynLib(sparseMatrixStats) 84 | -------------------------------------------------------------------------------- /man/colVarDiffs-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colVarDiffs,dgCMatrix-method} 4 | \alias{colVarDiffs,dgCMatrix-method} 5 | \alias{rowVarDiffs,dgCMatrix-method} 6 | \title{Calculates the variance of the difference between each element of a row 7 | (column) of a matrix-like object} 8 | \usage{ 9 | \S4method{colVarDiffs}{dgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | na.rm = FALSE, 14 | diff = 1L, 15 | trim = 0, 16 | useNames = TRUE 17 | ) 18 | 19 | \S4method{rowVarDiffs}{dgCMatrix}( 20 | x, 21 | rows = NULL, 22 | cols = NULL, 23 | na.rm = FALSE, 24 | diff = 1L, 25 | trim = 0, 26 | useNames = TRUE 27 | ) 28 | } 29 | \arguments{ 30 | \item{x}{An NxK matrix-like object.} 31 | 32 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 33 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 34 | done.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{diff}{An integer specifying the order of difference.} 41 | 42 | \item{trim}{A double in [0,1/2] specifying the fraction of observations to 43 | be trimmed from each end of (sorted) x before estimation.} 44 | 45 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 46 | } 47 | \value{ 48 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 49 | } 50 | \description{ 51 | Calculates the variance of the difference between each element of a row 52 | (column) of a matrix-like object. 53 | } 54 | \details{ 55 | The S4 methods for \code{x} of type \code{\link{matrix}}, 56 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 57 | \code{matrixStats::\link[matrixStats]{rowVarDiffs}} / 58 | \code{matrixStats::\link[matrixStats]{colVarDiffs}}. 59 | } 60 | \examples{ 61 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 62 | mat[2, 1] <- NA 63 | mat[3, 3] <- Inf 64 | mat[4, 1] <- 0 65 | 66 | print(mat) 67 | 68 | rowVarDiffs(mat) 69 | colVarDiffs(mat) 70 | } 71 | \seealso{ 72 | \itemize{ 73 | \item \code{matrixStats::\link[matrixStats:varDiff]{rowVarDiffs}()} and 74 | \code{matrixStats::\link[matrixStats:varDiff]{colVarDiffs}()} which 75 | are used when the input is a \code{matrix} or \code{numeric} vector. 76 | \item for the direct variance see \code{\link[MatrixGenerics:rowVars]{rowVars()}}. 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /man/colSdDiffs-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colSdDiffs,dgCMatrix-method} 4 | \alias{colSdDiffs,dgCMatrix-method} 5 | \alias{rowSdDiffs,dgCMatrix-method} 6 | \title{Calculates the standard deviation of the difference between each element of 7 | a row (column) of a matrix-like object} 8 | \usage{ 9 | \S4method{colSdDiffs}{dgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | na.rm = FALSE, 14 | diff = 1L, 15 | trim = 0, 16 | useNames = TRUE 17 | ) 18 | 19 | \S4method{rowSdDiffs}{dgCMatrix}( 20 | x, 21 | rows = NULL, 22 | cols = NULL, 23 | na.rm = FALSE, 24 | diff = 1L, 25 | trim = 0, 26 | useNames = TRUE 27 | ) 28 | } 29 | \arguments{ 30 | \item{x}{An NxK matrix-like object.} 31 | 32 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 33 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 34 | done.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{diff}{An integer specifying the order of difference.} 41 | 42 | \item{trim}{A double in [0,1/2] specifying the fraction of observations to 43 | be trimmed from each end of (sorted) x before estimation.} 44 | 45 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 46 | } 47 | \value{ 48 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 49 | } 50 | \description{ 51 | Calculates the standard deviation of the difference between each element of 52 | a row (column) of a matrix-like object. 53 | } 54 | \details{ 55 | The S4 methods for \code{x} of type \code{\link{matrix}}, 56 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 57 | \code{matrixStats::\link[matrixStats]{rowSdDiffs}} / 58 | \code{matrixStats::\link[matrixStats]{colSdDiffs}}. 59 | } 60 | \examples{ 61 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 62 | mat[2, 1] <- NA 63 | mat[3, 3] <- Inf 64 | mat[4, 1] <- 0 65 | 66 | print(mat) 67 | 68 | rowSdDiffs(mat) 69 | colSdDiffs(mat) 70 | } 71 | \seealso{ 72 | \itemize{ 73 | \item \code{matrixStats::\link[matrixStats:varDiff]{rowSdDiffs}()} and 74 | \code{matrixStats::\link[matrixStats:varDiff]{colSdDiffs}()} which are 75 | used when the input is a \code{matrix} or \code{numeric} vector. 76 | \item for the direct standard deviation see \code{\link[MatrixGenerics:rowSds]{rowSds()}}. 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /man/colIQRDiffs-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colIQRDiffs,dgCMatrix-method} 4 | \alias{colIQRDiffs,dgCMatrix-method} 5 | \alias{rowIQRDiffs,dgCMatrix-method} 6 | \title{Calculates the interquartile range of the difference between each element of 7 | a row (column) of a matrix-like object} 8 | \usage{ 9 | \S4method{colIQRDiffs}{dgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | na.rm = FALSE, 14 | diff = 1L, 15 | trim = 0, 16 | useNames = TRUE 17 | ) 18 | 19 | \S4method{rowIQRDiffs}{dgCMatrix}( 20 | x, 21 | rows = NULL, 22 | cols = NULL, 23 | na.rm = FALSE, 24 | diff = 1L, 25 | trim = 0, 26 | useNames = TRUE 27 | ) 28 | } 29 | \arguments{ 30 | \item{x}{An NxK matrix-like object.} 31 | 32 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 33 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 34 | done.} 35 | 36 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 37 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 38 | omitted from the calculations.} 39 | 40 | \item{diff}{An integer specifying the order of difference.} 41 | 42 | \item{trim}{A double in [0,1/2] specifying the fraction of observations to 43 | be trimmed from each end of (sorted) x before estimation.} 44 | 45 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 46 | } 47 | \value{ 48 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 49 | } 50 | \description{ 51 | Calculates the interquartile range of the difference between each element of 52 | a row (column) of a matrix-like object. 53 | } 54 | \details{ 55 | The S4 methods for \code{x} of type \code{\link{matrix}}, 56 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 57 | \code{matrixStats::\link[matrixStats]{rowIQRDiffs}} / 58 | \code{matrixStats::\link[matrixStats]{colIQRDiffs}}. 59 | } 60 | \examples{ 61 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 62 | mat[2, 1] <- NA 63 | mat[3, 3] <- Inf 64 | mat[4, 1] <- 0 65 | 66 | print(mat) 67 | 68 | rowIQRDiffs(mat) 69 | colIQRDiffs(mat) 70 | } 71 | \seealso{ 72 | \itemize{ 73 | \item \code{matrixStats::\link[matrixStats:varDiff]{rowIQRDiffs}()} and 74 | \code{matrixStats::\link[matrixStats:varDiff]{colIQRDiffs}()} which 75 | are used when the input is a \code{matrix} or \code{numeric} vector. 76 | \item For the direct interquartile range see also \link[MatrixGenerics]{rowIQRs}. 77 | } 78 | } 79 | -------------------------------------------------------------------------------- /man/colMads-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colMads,dgCMatrix-method} 4 | \alias{colMads,dgCMatrix-method} 5 | \alias{rowMads,dgCMatrix-method} 6 | \title{Calculates the median absolute deviation for each row (column) of a 7 | matrix-like object} 8 | \usage{ 9 | \S4method{colMads}{dgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | center = NULL, 14 | constant = 1.4826, 15 | na.rm = FALSE, 16 | useNames = TRUE 17 | ) 18 | 19 | \S4method{rowMads}{dgCMatrix}( 20 | x, 21 | rows = NULL, 22 | cols = NULL, 23 | center = NULL, 24 | constant = 1.4826, 25 | na.rm = FALSE, 26 | useNames = TRUE 27 | ) 28 | } 29 | \arguments{ 30 | \item{x}{An NxK matrix-like object.} 31 | 32 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 33 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 34 | done.} 35 | 36 | \item{center}{(optional) the center, defaults to the row means} 37 | 38 | \item{constant}{A scale factor. See \code{stats::\link[stats]{mad}()} for 39 | details.} 40 | 41 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 42 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 43 | omitted from the calculations.} 44 | 45 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 46 | } 47 | \value{ 48 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 49 | } 50 | \description{ 51 | Calculates the median absolute deviation for each row (column) of a 52 | matrix-like object. 53 | } 54 | \details{ 55 | The S4 methods for \code{x} of type \code{\link{matrix}}, 56 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 57 | \code{matrixStats::\link[matrixStats]{rowMads}} / 58 | \code{matrixStats::\link[matrixStats]{colMads}}. 59 | } 60 | \examples{ 61 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 62 | mat[2, 1] <- NA 63 | mat[3, 3] <- Inf 64 | mat[4, 1] <- 0 65 | 66 | print(mat) 67 | 68 | rowMads(mat) 69 | colMads(mat) 70 | } 71 | \seealso{ 72 | \itemize{ 73 | \item \code{matrixStats::\link[matrixStats:rowSds]{rowMads}()} and 74 | \code{matrixStats::\link[matrixStats:rowSds]{colMads}()} which are used 75 | when the input is a \code{matrix} or \code{numeric} vector. 76 | \item For mean estimates, see \code{\link[MatrixGenerics]{rowMeans2}()} and 77 | \code{\link[base:colSums]{rowMeans}()}. 78 | \item For non-robust standard deviation estimates, see 79 | \code{\link[MatrixGenerics]{rowSds}()}. 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /man/colQuantiles-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colQuantiles,xgCMatrix-method} 4 | \alias{colQuantiles,xgCMatrix-method} 5 | \alias{rowQuantiles,xgCMatrix-method} 6 | \title{Calculates quantiles for each row (column) of a matrix-like object} 7 | \usage{ 8 | \S4method{colQuantiles}{xgCMatrix}( 9 | x, 10 | rows = NULL, 11 | cols = NULL, 12 | probs = seq(from = 0, to = 1, by = 0.25), 13 | na.rm = FALSE, 14 | type = 7L, 15 | useNames = TRUE, 16 | drop = TRUE 17 | ) 18 | 19 | \S4method{rowQuantiles}{xgCMatrix}( 20 | x, 21 | rows = NULL, 22 | cols = NULL, 23 | probs = seq(from = 0, to = 1, by = 0.25), 24 | na.rm = FALSE, 25 | type = 7L, 26 | useNames = TRUE, 27 | drop = TRUE 28 | ) 29 | } 30 | \arguments{ 31 | \item{x}{An NxK matrix-like object.} 32 | 33 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 34 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 35 | done.} 36 | 37 | \item{probs}{A numeric vector of J probabilities in [0, 1].} 38 | 39 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 40 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 41 | omitted from the calculations.} 42 | 43 | \item{type}{An integer specifying the type of estimator. See 44 | \code{stats::\link[stats]{quantile}()}. for more details.} 45 | 46 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 47 | 48 | \item{drop}{If \code{TRUE} a vector is returned if \code{J == 1}.} 49 | } 50 | \value{ 51 | a \code{\link{numeric}} \code{NxJ} (\code{KxJ}) 52 | \code{\link{matrix}}, where N (K) is the number of rows (columns) for 53 | which the J values are calculated. 54 | } 55 | \description{ 56 | Calculates quantiles for each row (column) of a matrix-like object. 57 | } 58 | \details{ 59 | The S4 methods for \code{x} of type \code{\link{matrix}}, 60 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 61 | \code{matrixStats::\link[matrixStats]{rowQuantiles}} / 62 | \code{matrixStats::\link[matrixStats]{colQuantiles}}. 63 | } 64 | \examples{ 65 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 66 | mat[2, 1] <- NA 67 | mat[3, 3] <- Inf 68 | mat[4, 1] <- 0 69 | 70 | print(mat) 71 | 72 | rowQuantiles(mat) 73 | colQuantiles(mat) 74 | } 75 | \seealso{ 76 | \itemize{ 77 | \item \code{matrixStats::\link[matrixStats]{rowQuantiles}()} and 78 | \code{matrixStats::\link[matrixStats:rowQuantiles]{colQuantiles}()} which 79 | are used when the input is a \code{matrix} or \code{numeric} vector. 80 | \item \link[stats:quantile]{stats::quantile} 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /man/colMadDiffs-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colMadDiffs,dgCMatrix-method} 4 | \alias{colMadDiffs,dgCMatrix-method} 5 | \alias{rowMadDiffs,dgCMatrix-method} 6 | \title{Calculates the mean absolute deviation of the difference between each 7 | element of a row (column) of a matrix-like object} 8 | \usage{ 9 | \S4method{colMadDiffs}{dgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | na.rm = FALSE, 14 | diff = 1L, 15 | trim = 0, 16 | constant = 1.4826, 17 | ..., 18 | useNames = TRUE 19 | ) 20 | 21 | \S4method{rowMadDiffs}{dgCMatrix}( 22 | x, 23 | rows = NULL, 24 | cols = NULL, 25 | na.rm = FALSE, 26 | diff = 1L, 27 | trim = 0, 28 | constant = 1.4826, 29 | ..., 30 | useNames = TRUE 31 | ) 32 | } 33 | \arguments{ 34 | \item{x}{An NxK matrix-like object.} 35 | 36 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 37 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 38 | done.} 39 | 40 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 41 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 42 | omitted from the calculations.} 43 | 44 | \item{diff}{An integer specifying the order of difference.} 45 | 46 | \item{trim}{A double in [0,1/2] specifying the fraction of observations to 47 | be trimmed from each end of (sorted) x before estimation.} 48 | 49 | \item{constant}{A scale factor. See \code{\link{mad}} for details.} 50 | 51 | \item{...}{Additional arguments passed to specific methods.} 52 | 53 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 54 | } 55 | \value{ 56 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 57 | } 58 | \description{ 59 | Calculates the mean absolute deviation of the difference between each 60 | element of a row (column) of a matrix-like object. 61 | } 62 | \details{ 63 | The S4 methods for \code{x} of type \code{\link{matrix}}, 64 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 65 | \code{matrixStats::\link[matrixStats]{rowMadDiffs}} / 66 | \code{matrixStats::\link[matrixStats]{colMadDiffs}}. 67 | } 68 | \examples{ 69 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 70 | mat[2, 1] <- NA 71 | mat[3, 3] <- Inf 72 | mat[4, 1] <- 0 73 | 74 | print(mat) 75 | 76 | rowMadDiffs(mat) 77 | colMadDiffs(mat) 78 | } 79 | \seealso{ 80 | \itemize{ 81 | \item \code{matrixStats::\link[matrixStats:varDiff]{rowMadDiffs}()} and 82 | \code{matrixStats::\link[matrixStats:varDiff]{colMadDiffs}()} which 83 | are used when the input is a \code{matrix} or \code{numeric} vector. 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /man/colRanges-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colRanges,dgCMatrix-method} 4 | \alias{colRanges,dgCMatrix-method} 5 | \alias{rowRanges,dgCMatrix-method} 6 | \title{Calculates the minimum and maximum for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colRanges}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 10 | 11 | \S4method{rowRanges}{dgCMatrix}(x, rows = NULL, cols = NULL, na.rm = FALSE, useNames = TRUE) 12 | } 13 | \arguments{ 14 | \item{x}{An NxK matrix-like object.} 15 | 16 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 17 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 18 | done.} 19 | 20 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 21 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 22 | omitted from the calculations.} 23 | 24 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 25 | } 26 | \value{ 27 | a \code{\link{numeric}} \code{Nx2} (\code{Kx2}) 28 | \code{\link{matrix}}, where N (K) is the number of rows (columns) for 29 | which the ranges are calculated. 30 | } 31 | \description{ 32 | Calculates the minimum and maximum for each row (column) of a matrix-like 33 | object. 34 | } 35 | \details{ 36 | The S4 methods for \code{x} of type \code{\link{matrix}}, 37 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 38 | \code{matrixStats::\link[matrixStats]{rowRanges}} / 39 | \code{matrixStats::\link[matrixStats]{colRanges}}. 40 | } 41 | \note{ 42 | Unfortunately for the argument list of the \code{rowRanges()} 43 | generic function we cannot follow the scheme used for the other 44 | row/column matrix summarization generic functions. This is because 45 | we need to be compatible with the historic \code{rowRanges()} getter 46 | for \link[SummarizedExperiment]{RangedSummarizedExperiment} objects. 47 | See \code{?SummarizedExperiment::\link[SummarizedExperiment]{rowRanges}}. 48 | } 49 | \examples{ 50 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 51 | mat[2, 1] <- NA 52 | mat[3, 3] <- Inf 53 | mat[4, 1] <- 0 54 | 55 | print(mat) 56 | 57 | rowRanges(mat) 58 | colRanges(mat) 59 | } 60 | \seealso{ 61 | \itemize{ 62 | \item \code{matrixStats::\link[matrixStats]{rowRanges}()} and 63 | \code{matrixStats::\link[matrixStats:rowRanges]{colRanges}()} which are 64 | used when the input is a \code{matrix} or \code{numeric} vector. 65 | \item For max estimates, see \code{\link[MatrixGenerics]{rowMaxs}()}. 66 | \item For min estimates, see \code{\link[MatrixGenerics]{rowMins}()}. 67 | \item \code{base::\link{range}()}. 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /man/colWeightedMads-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colWeightedMads,dgCMatrix-method} 4 | \alias{colWeightedMads,dgCMatrix-method} 5 | \alias{rowWeightedMads,dgCMatrix-method} 6 | \title{Calculates the weighted median absolute deviation for each row (column) of a 7 | matrix-like object} 8 | \usage{ 9 | \S4method{colWeightedMads}{dgCMatrix}( 10 | x, 11 | w = NULL, 12 | rows = NULL, 13 | cols = NULL, 14 | na.rm = FALSE, 15 | constant = 1.4826, 16 | center = NULL, 17 | useNames = TRUE 18 | ) 19 | 20 | \S4method{rowWeightedMads}{dgCMatrix}( 21 | x, 22 | w = NULL, 23 | rows = NULL, 24 | cols = NULL, 25 | na.rm = FALSE, 26 | constant = 1.4826, 27 | center = NULL, 28 | useNames = TRUE 29 | ) 30 | } 31 | \arguments{ 32 | \item{x}{An NxK matrix-like object.} 33 | 34 | \item{w}{A \code{\link{numeric}} vector of length K (N) that specifies by 35 | how much each element is weighted.} 36 | 37 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 38 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 39 | done.} 40 | 41 | \item{na.rm}{If \code{\link[base]{TRUE}}, missing values 42 | (\code{\link[base]{NA}} or \code{\link[base]{NaN}}) are 43 | omitted from the calculations.} 44 | 45 | \item{constant}{A scale factor. See \code{stats::\link[stats]{mad}()} for 46 | details.} 47 | 48 | \item{center}{Not supported at the moment.} 49 | 50 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 51 | } 52 | \value{ 53 | Returns a \code{\link{numeric}} \code{\link{vector}} of length N (K). 54 | } 55 | \description{ 56 | Calculates the weighted median absolute deviation for each row (column) of 57 | a matrix-like object. 58 | } 59 | \details{ 60 | The S4 methods for \code{x} of type \code{\link{matrix}}, 61 | \code{\link{array}}, \code{\link{table}}, or \code{\link{numeric}} call 62 | \code{matrixStats::\link[matrixStats]{rowWeightedMads}} / 63 | \code{matrixStats::\link[matrixStats]{colWeightedMads}}. 64 | } 65 | \examples{ 66 | mat <- matrix(0, nrow=10, ncol=5) 67 | mat[sample(prod(dim(mat)), 25)] <- rpois(n=25, 5) 68 | sp_mat <- as(mat, "dgCMatrix") 69 | weights <- rnorm(10, mean=1, sd=0.1) 70 | 71 | # sparse version 72 | sparseMatrixStats::colWeightedMads(sp_mat, weights) 73 | 74 | # Attention the result differs from matrixStats 75 | # because it always uses 'interpolate=FALSE'. 76 | matrixStats::colWeightedMads(mat, weights) 77 | 78 | } 79 | \seealso{ 80 | \itemize{ 81 | \item \code{matrixStats::\link[matrixStats:weightedMad]{rowWeightedMads}()} and 82 | \code{matrixStats::\link[matrixStats:weightedMad]{colWeightedMads}()} 83 | which are used when the input is a \code{matrix} or \code{numeric} vector. 84 | \item See also \link[MatrixGenerics]{rowMads} for the corresponding unweighted function. 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /man/colAvgsPerRowSet-xgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colAvgsPerRowSet,xgCMatrix-method} 4 | \alias{colAvgsPerRowSet,xgCMatrix-method} 5 | \alias{colAvgsPerRowSet} 6 | \alias{rowAvgsPerColSet,xgCMatrix-method} 7 | \title{Calculates for each row (column) a summary statistic for equally sized subsets of columns (rows)} 8 | \usage{ 9 | \S4method{colAvgsPerRowSet}{xgCMatrix}( 10 | X, 11 | W = NULL, 12 | cols = NULL, 13 | S, 14 | FUN = colMeans2, 15 | ..., 16 | na.rm = NA, 17 | tFUN = FALSE 18 | ) 19 | 20 | \S4method{rowAvgsPerColSet}{xgCMatrix}( 21 | X, 22 | W = NULL, 23 | rows = NULL, 24 | S, 25 | FUN = rowMeans2, 26 | ..., 27 | na.rm = NA, 28 | tFUN = FALSE 29 | ) 30 | } 31 | \arguments{ 32 | \item{X}{An \code{NxM} matrix-like object.} 33 | 34 | \item{W}{An optional numeric \code{NxM} matrix of weights.} 35 | 36 | \item{S}{An \link{integer} \code{KxJ} matrix that specifying the \code{J} subsets. Each 37 | column hold \code{K} column (row) indices for the corresponding subset. The 38 | range of values is [1, M] ([1,N]).} 39 | 40 | \item{FUN}{A row-by-row (column-by-column) summary statistic function. It is 41 | applied to to each column (row) subset of \code{X} that is specified by \code{S}.} 42 | 43 | \item{...}{Additional arguments passed to \code{FUN}.} 44 | 45 | \item{na.rm}{(logical) Argument passed to \code{FUN()} as \code{na.rm = na.rm}. 46 | If \code{NA} (default), then \code{na.rm = TRUE} is used if \code{X} or \code{S} holds missing values, 47 | otherwise \code{na.rm = FALSE}.} 48 | 49 | \item{tFUN}{If \code{TRUE}, \code{X} is transposed before it is passed to \code{FUN}.} 50 | 51 | \item{rows, cols}{A \code{\link{vector}} indicating the subset (and/or 52 | columns) to operate over. If \code{\link{NULL}}, no subsetting is 53 | done.} 54 | } 55 | \value{ 56 | Returns a numeric \code{JxN} (\code{MxJ}) matrix. 57 | } 58 | \description{ 59 | Calculates for each row (column) a summary statistic for equally sized subsets of columns (rows) 60 | } 61 | \details{ 62 | **Note**: the handling of missing parameters differs from 63 | [matrixStats::colAvgsPerRowSet()]. The `matrixStats` version 64 | always removes `NA`'s if there are any in the data. This method 65 | however does whatever is passed in the `...` parameter. 66 | } 67 | \examples{ 68 | mat <- matrix(rnorm(20), nrow = 5, ncol = 4) 69 | mat[2, 1] <- NA 70 | mat[3, 3] <- Inf 71 | mat[4, 1] <- 0 72 | 73 | print(mat) 74 | 75 | S <- matrix(1:ncol(mat), ncol = 2) 76 | print(S) 77 | 78 | rowAvgsPerColSet(mat, S = S, FUN = rowMeans) 79 | rowAvgsPerColSet(mat, S = S, FUN = rowVars) 80 | } 81 | \seealso{ 82 | \itemize{ 83 | \item \code{matrixStats::\link[matrixStats:rowAvgsPerColSet]{rowAvgsPerColSet}()} 84 | and \code{matrixStats::\link[matrixStats:rowAvgsPerColSet]{colAvgsPerRowSet}()} 85 | which are used when the input is a \code{matrix} or \code{numeric} vector. 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/VectorSubsetView.h: -------------------------------------------------------------------------------- 1 | #ifndef VectorSubsetView_h 2 | #define VectorSubsetView_h 3 | 4 | 5 | #include 6 | #include 7 | using namespace Rcpp; 8 | // [[Rcpp::plugins("cpp11")]] 9 | 10 | 11 | template 12 | class VectorSubsetView { 13 | typedef typename Rcpp::Vector RcppVector; 14 | RcppVector vec; 15 | public: 16 | const R_len_t start; 17 | const R_len_t size_m; 18 | typedef typename RcppVector::Proxy Proxy ; 19 | typedef typename RcppVector::Storage stored_type; 20 | 21 | class postinc_return { 22 | Proxy value; 23 | public: 24 | postinc_return(Proxy value_): value(value_) {} 25 | Proxy operator*(){return value; } 26 | }; 27 | 28 | class iterator { 29 | VectorSubsetView* vsv; 30 | R_len_t current; 31 | public: 32 | using iterator_category = std::forward_iterator_tag; 33 | using value_type = stored_type; 34 | using reference = stored_type; 35 | using pointer = stored_type const * ; 36 | using difference_type = ptrdiff_t; 37 | 38 | 39 | iterator(VectorSubsetView* vsv_): vsv(vsv_), current(0) {} 40 | 41 | Proxy operator*() const { 42 | return vsv->vec[vsv->start + current]; 43 | } 44 | 45 | iterator& operator++(){ //preincrement 46 | ++current; 47 | if(current == vsv->size()){ 48 | vsv = nullptr; 49 | } 50 | return *this; 51 | } 52 | 53 | postinc_return operator++(int){ 54 | postinc_return temp(vsv->vec[vsv->start + current]); 55 | ++*this; 56 | return temp; 57 | } 58 | 59 | friend bool operator==(iterator const& lhs,iterator const& rhs){ 60 | return lhs.vsv == rhs.vsv; 61 | } 62 | friend bool operator!=(iterator const& lhs,iterator const& rhs){ 63 | return !(lhs==rhs); 64 | } 65 | 66 | }; 67 | 68 | VectorSubsetView(const RcppVector vec_, const R_len_t start_, const R_len_t end_): 69 | vec(vec_), start(start_), size_m(end_ - start_) { 70 | if(end_ < start_){ 71 | throw std::range_error("End must not be smaller than start"); 72 | } 73 | if(start_ < 0){ 74 | throw std::range_error("Start must not be smaller than 0"); 75 | } 76 | if(end_ > vec.size()){ 77 | throw std::range_error("End must not be larger than size of vec"); 78 | } 79 | } 80 | 81 | iterator begin() { 82 | if(size_m == 0){ 83 | return iterator(nullptr); 84 | }else{ 85 | return iterator(this); 86 | } 87 | } 88 | iterator end() { return iterator(nullptr); } 89 | 90 | R_len_t size() { return size_m; } 91 | 92 | bool is_empty(){ 93 | return size_m == 0; 94 | } 95 | 96 | Proxy operator[](R_len_t i) { 97 | return vec[start + i]; 98 | } 99 | 100 | }; 101 | 102 | 103 | 104 | 105 | // You can include R code blocks in C++ files processed with sourceCpp 106 | // (useful for testing and development). The R code will be automatically 107 | // run after the compilation. 108 | // 109 | 110 | /*** R 111 | vec <- 1:10 112 | subset_sum(vec) 113 | */ 114 | 115 | 116 | 117 | # endif /* VectorSubsetView_h */ 118 | -------------------------------------------------------------------------------- /NEWS: -------------------------------------------------------------------------------- 1 | Changes in version 1.17 2 | + Fix handling of missing values in `rowSums2` if `cols` is a boolean vector 3 | + Implement optimized code path for `rowSums2` and `rowMeans2` if `cols` is provided 4 | 5 | Changes in version 1.15 6 | + Throw error if length of 'center' in colVars, colSds, colMads (and 7 | corrresponding row-functions) does not match size of 'x'. 8 | This behavior was never supported by `sparseMatrixStats` and previously 9 | could return incorrect results. This change matches the upcoming behavior of 10 | matrixStats (https://github.com/HenrikBengtsson/matrixStats/issues/254). 11 | 12 | 13 | Changes in version 1.13 14 | + Make sparseMatrixStats compatible with matrixStats release v1.0.0. 15 | In particular change 'useNames'to 'TRUE' by default. 16 | + Add fast path for 'rowSums2(x, cols = logical_vector)' 17 | + Add useNames parameter to all functions 18 | + fix incomplete method signature of rowQuantiles 19 | + 20 | 21 | Changes in version 1.3 22 | + Avoid C++14 features. This should fix the reoccuring installation problems. 23 | + Support center argument in colVars (and friends). This can speed up 24 | the calculation if both are needed. Note that center must be a proper 25 | estimate of the column means. 26 | + Fix bug that broke test on ARM architectures 27 | + Add extensive section on how to fix installation problems to README 28 | + Fix compatibility with matrixStats v.0.58.0 29 | + Fix bug in colCounts, colAnys, colAlls, colRanks if the sparse matrix 30 | contained explicit zeros. 31 | 32 | 33 | Changes in version 1.2 34 | + Use numerically stable algorithms for sum, mean, and variance 35 | + Implement dedicated function for rowSums2, rowMeans2, rowVars 36 | which avoid transposing the input matrix to call the colXXX version 37 | + Support logical input (lgCMatrix) for all methods that matrixStats 38 | supports. Introduce new class xgCMatrix as union class of dgCMatrix 39 | and lgCMatrix. 40 | + Fix bug in colWeightedVars() that returned slightly off results on 41 | Linux, because there abs() internally returns integers instead of 42 | doubles 43 | + Change argument name in colRanks from `preserve.shape` to 44 | `preserveShape` to match matrixStats (thanks @LTLA for reporting) 45 | + Fix bug in low-level `quantile_sparse()` related to +- Inf 46 | (thanks @LTLA for reporting) 47 | + Move MatrixGenerics from Imports to Depends so that loading 48 | matrixStats does not break calls to sparseMatrixStats 49 | (thanks @hpages for reporting) 50 | + Fix bug in [col|row]Quantile which ignored drop argument if 51 | length(probs) == 1 52 | 53 | Changes in version 1.0.5 (2020-05-17) 54 | + Fix links in documentation to get rid of WARNINGS 55 | 56 | Changes in version 1.0.4 (2020-05-17) 57 | + Fix bugs in colTabulates 58 | + Update documentation to avoid warnings in build on Windows 59 | 60 | Changes in version 1.0.3 (2020-05-17) 61 | + Fix bug in colAnys and colAlls if value = TRUE 62 | 63 | Changes in version 1.0.2 (2020-05-10) 64 | + Fix bug in colMaxs, colMins related to missing values 65 | 66 | Changes in version 1.0.1 (2020-05-08) 67 | + Fix bugs in colMaxs, colMins, colAnys 68 | + Fix bug in colLogSumExps 69 | 70 | 71 | Changes in version 0.0.99 (2020-04-03) 72 | + Submitted to Bioconductor 73 | -------------------------------------------------------------------------------- /man/colRanks-dgCMatrix-method.Rd: -------------------------------------------------------------------------------- 1 | % Generated by roxygen2: do not edit by hand 2 | % Please edit documentation in R/methods.R, R/methods_row.R 3 | \name{colRanks,dgCMatrix-method} 4 | \alias{colRanks,dgCMatrix-method} 5 | \alias{rowRanks,dgCMatrix-method} 6 | \title{Calculates the rank of the elements for each row (column) of a matrix-like 7 | object} 8 | \usage{ 9 | \S4method{colRanks}{dgCMatrix}( 10 | x, 11 | rows = NULL, 12 | cols = NULL, 13 | ties.method = c("max", "average", "min"), 14 | preserveShape = FALSE, 15 | na.handling = c("keep", "last"), 16 | ..., 17 | useNames = TRUE 18 | ) 19 | 20 | \S4method{rowRanks}{dgCMatrix}( 21 | x, 22 | rows = NULL, 23 | cols = NULL, 24 | ties.method = c("max", "average", "min"), 25 | preserveShape = TRUE, 26 | na.handling = c("keep", "last"), 27 | ..., 28 | useNames = TRUE 29 | ) 30 | } 31 | \arguments{ 32 | \item{x}{An NxK matrix-like object.} 33 | 34 | \item{rows, cols}{A \code{\link{vector}} indicating the subset of rows 35 | (and/or columns) to operate over. If \code{\link{NULL}}, no subsetting is 36 | done.} 37 | 38 | \item{ties.method}{A character string specifying how ties are treated. Note 39 | that the default specifies fewer options than the original matrixStats 40 | package.} 41 | 42 | \item{preserveShape}{a boolean that specifies if the returned matrix has the same 43 | dimensions as the input matrix. By default this is true for `rowRanks()`, but false for 44 | `colRanks()`.} 45 | 46 | \item{na.handling}{string specifying how `NA`s are handled. They can either be preserved with an `NA` rank 47 | ('keep') or sorted in at the end ('last'). Default is 'keep' derived from the behavior of the equivalent} 48 | 49 | \item{...}{Additional arguments passed to specific methods.} 50 | 51 | \item{useNames}{If \code{\link{TRUE}} (default), names attributes of result are set. Else if \code{\link{FALSE}}, no naming support is done.} 52 | } 53 | \value{ 54 | a matrix of type \code{\link{integer}} is returned unless 55 | \code{ties.method = "average"}. Ithas dimensions` \code{NxJ} (\code{KxJ}) 56 | \code{\link{matrix}}, where N (K) is the number of rows (columns) of the 57 | input x. 58 | } 59 | \description{ 60 | Calculates the rank of the elements for each row (column) of a matrix-like 61 | object. 62 | } 63 | \details{ 64 | There are three different methods available for handling ties: 65 | \describe{ 66 | \item{`max`}{for values with identical values the maximum rank is returned} 67 | \item{`average`}{for values with identical values the average of the ranks they cover 68 | is returned. Note, that in this case the return value is of type `numeric`.} 69 | \item{`min`}{for values with identical values the minimum rank is returned.} 70 | } 71 | } 72 | \examples{ 73 | mat <- matrix(rnorm(15), nrow = 5, ncol = 3) 74 | mat[2, 1] <- NA 75 | mat[3, 3] <- Inf 76 | mat[4, 1] <- 0 77 | 78 | print(mat) 79 | 80 | rowRanks(mat) 81 | colRanks(mat) 82 | } 83 | \seealso{ 84 | \itemize{ 85 | \item \code{matrixStats::\link[matrixStats]{rowRanks}()} and 86 | \code{matrixStats::\link[matrixStats:rowRanks]{colRanks}()} which are used 87 | when the input is a \code{matrix} or \code{numeric} vector. 88 | \item \link[base:rank]{base::rank} 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /src/SkipNAVectorSubsetView.h: -------------------------------------------------------------------------------- 1 | 2 | 3 | #ifndef SkipNAVectorSubsetView_h 4 | #define SkipNAVectorSubsetView_h 5 | 6 | #include 7 | #include "VectorSubsetView.h" 8 | using namespace Rcpp; 9 | 10 | 11 | template 12 | class SkipNAVectorSubsetView { 13 | typedef typename Rcpp::Vector RcppVector; 14 | VectorSubsetView* vsv; 15 | public: 16 | typedef typename RcppVector::Proxy Proxy ; 17 | typedef typename RcppVector::Storage stored_type; 18 | 19 | class postinc_return { 20 | Proxy value; 21 | public: 22 | postinc_return(Proxy value_): value(value_) {} 23 | Proxy operator*(){return value; } 24 | }; 25 | 26 | class iterator { 27 | typedef typename VectorSubsetView::iterator vsv_iterator; 28 | vsv_iterator vsv_iter_start; 29 | vsv_iterator vsv_iter_end; 30 | public: 31 | using iterator_category = std::forward_iterator_tag; 32 | using value_type = stored_type; 33 | using reference = stored_type; 34 | using pointer = stored_type const * ; 35 | using difference_type = ptrdiff_t; 36 | 37 | 38 | iterator(vsv_iterator vsv_iter_start_, vsv_iterator vsv_iter_end_): vsv_iter_start(vsv_iter_start_), vsv_iter_end(vsv_iter_end_) { 39 | while(vsv_iter_start != vsv_iter_end && RcppVector::is_na(*vsv_iter_start)){ 40 | ++vsv_iter_start; 41 | } 42 | } 43 | 44 | 45 | Proxy operator*() const { 46 | return *(vsv_iter_start); 47 | } 48 | 49 | iterator& operator++(){ //preincrement 50 | ++vsv_iter_start; 51 | if(vsv_iter_start == vsv_iter_end){ 52 | vsv_iter_start = nullptr; 53 | return *this; 54 | }else if(RcppVector::is_na(*vsv_iter_start)){ 55 | return ++*this; 56 | }else{ 57 | return *this; 58 | } 59 | } 60 | 61 | postinc_return operator++(int){ 62 | postinc_return temp(*vsv_iter_start); 63 | ++*this; 64 | return temp; 65 | } 66 | 67 | friend bool operator==(iterator const& lhs,iterator const& rhs){ 68 | return lhs.vsv_iter_start == rhs.vsv_iter_start; 69 | } 70 | friend bool operator!=(iterator const& lhs,iterator const& rhs){ 71 | return !(lhs==rhs); 72 | } 73 | 74 | }; 75 | 76 | SkipNAVectorSubsetView(VectorSubsetView* vsv_): vsv(vsv_) {} 77 | 78 | iterator begin() { 79 | return iterator(vsv->begin(), vsv->end()); 80 | } 81 | iterator end() { 82 | return iterator(nullptr, nullptr); 83 | } 84 | 85 | bool is_empty(){ 86 | return begin() == end(); 87 | } 88 | 89 | R_len_t size(){ 90 | R_len_t result = 0; 91 | for(iterator index = begin(); index != end(); index++){ 92 | result++; 93 | } 94 | return result; 95 | } 96 | 97 | }; 98 | 99 | // // [[Rcpp::export]] 100 | // double subset_sum_without_na(NumericVector v, R_len_t start, R_len_t end){ 101 | // auto vsv = VectorSubsetView(v, start, end); 102 | // int s = vsv.size; 103 | // for(double e: vsv){ 104 | // Rcout << e << std::endl; 105 | // } 106 | // return std::accumulate(vsv.begin(), vsv.end(), 0.0); 107 | // } 108 | 109 | 110 | 111 | #endif /* SkipNAVectorSubsetView_h */ 112 | -------------------------------------------------------------------------------- /src/quantile.h: -------------------------------------------------------------------------------- 1 | #ifndef quantile_h 2 | #define quantile_h 3 | 4 | 5 | #include 6 | #include "VectorSubsetView.h" 7 | #include 8 | 9 | using namespace Rcpp; 10 | 11 | // ATTENTION: This method assumes that NA's have already been handled! 12 | template 13 | double quantile_sparse_impl(T values, int number_of_zeros, double prob){ 14 | if(prob < 0 || prob > 1){ 15 | throw std::range_error("prob must be between 0 and 1"); 16 | } 17 | R_len_t size = values.size(); 18 | int total_size = size + number_of_zeros; 19 | if(total_size == 0){ 20 | return NA_REAL; 21 | }else if(size == 0){ 22 | return 0.0; 23 | } 24 | double pivot = (total_size-1) * prob; 25 | // Rcout << "total_size: " << total_size << " pivot: " << pivot << std::endl; 26 | std::vector sorted_values; 27 | std::copy(values.begin(), values.end(), std::back_inserter(sorted_values)); 28 | std::sort(sorted_values.begin(), sorted_values.end()); 29 | double left_of_pivot = NA_REAL; 30 | double right_of_pivot = NA_REAL; 31 | bool left_of_zero = sorted_values[0] < 0; 32 | bool right_of_zero = !left_of_zero && number_of_zeros == 0; 33 | int zero_counter = ! left_of_zero && ! right_of_zero; 34 | int vec_counter = 0; 35 | for(int i = 0; i < sorted_values.size() + number_of_zeros; i++){ 36 | // Rcout << i << " " << vec_counter << " " << zero_counter << " " << left_of_zero << " " << right_of_zero << " " << std::endl; 37 | if(i == std::floor(pivot)){ 38 | if(! left_of_zero && ! right_of_zero){ 39 | left_of_pivot = 0; 40 | }else{ 41 | left_of_pivot = sorted_values[vec_counter]; 42 | } 43 | // Rcout << "left of pivot: " << i << " " << left_of_pivot << std::endl; 44 | } 45 | if(i == std::ceil(pivot)){ 46 | if(! left_of_zero && ! right_of_zero){ 47 | right_of_pivot = 0; 48 | }else{ 49 | right_of_pivot = sorted_values[vec_counter]; 50 | } 51 | // Rcout << "right of pivot: " << i << " " << right_of_pivot << std::endl; 52 | break; 53 | } 54 | 55 | if(left_of_zero){ 56 | vec_counter++; 57 | if(vec_counter == size || sorted_values[vec_counter] > 0){ 58 | left_of_zero = false; 59 | } 60 | } 61 | if(right_of_zero){ 62 | vec_counter++; 63 | } 64 | if(! left_of_zero && ! right_of_zero){ 65 | zero_counter++; 66 | if(zero_counter > number_of_zeros){ 67 | right_of_zero = true; 68 | } 69 | } 70 | } 71 | if(left_of_pivot == R_NegInf && right_of_pivot == R_PosInf){ 72 | return R_NaN; 73 | }else if(left_of_pivot == R_NegInf){ 74 | return R_NegInf; 75 | }else if(right_of_pivot == R_PosInf){ 76 | return R_PosInf; 77 | }else{ 78 | return left_of_pivot + (right_of_pivot - left_of_pivot) * std::fmod(pivot, 1.0); 79 | } 80 | } 81 | 82 | // [[Rcpp::export]] 83 | double quantile_sparse(NumericVector values, int number_of_zeros, double prob){ 84 | VectorSubsetView vsv(values, 0, values.size()); 85 | return quantile_sparse_impl >(vsv, number_of_zeros, prob); 86 | } 87 | 88 | #endif /* quantile_h */ 89 | 90 | /*** R 91 | N <- rpois(1, lambda=2) 92 | vec <- rnorm(N) 93 | nz <- rpois(1, lambda=3) 94 | compl_vec <- sort(c(vec, rep(0, nz))) 95 | prob <- runif(1) 96 | q1 <- quantile(compl_vec, prob) 97 | q2 <- quantile_sparse(vec, nz, prob) 98 | q1 99 | q2 100 | testthat::expect_equal(unname(q1), q2) 101 | 102 | */ 103 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check-release.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - master 5 | pull_request: 6 | branches: 7 | - master 8 | schedule: 9 | - cron: '0 8 * * 5' 10 | 11 | name: R-CMD-check-release 12 | 13 | jobs: 14 | R-CMD-check-release: 15 | if: "! contains(github.event.head_commit.message, '[ci skip]')" 16 | 17 | runs-on: ${{ matrix.config.os }} 18 | container: ${{ matrix.config.image }} 19 | 20 | name: ${{ matrix.config.os }} (${{ matrix.config.r }} - ${{ matrix.config.bioc }} - ${{ matrix.config.image }}) 21 | 22 | strategy: 23 | fail-fast: false 24 | matrix: 25 | config: 26 | - { os: windows-latest, r: '4.1 ', bioc: 'devel'} 27 | - { os: macOS-latest, r: '4.1', bioc: 'devel'} 28 | - { os: ubuntu-latest, r: '4.1', bioc: 'devel'} 29 | 30 | env: 31 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 32 | CRAN: ${{ matrix.config.cran }} 33 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 34 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 35 | 36 | steps: 37 | - name: Check out repo 38 | uses: actions/checkout@v2 39 | 40 | - name: Set up R 41 | uses: r-lib/actions/setup-r@master 42 | with: 43 | r-version: ${{ matrix.config.r }} 44 | 45 | - name: Set up pandoc 46 | uses: r-lib/actions/setup-pandoc@master 47 | 48 | - name: Install remotes 49 | run: | 50 | install.packages('remotes') 51 | shell: Rscript {0} 52 | 53 | - name: Set BiocManager version 54 | run: | 55 | install.packages('BiocManager') 56 | BiocManager::install(version = "${{ matrix.config.bioc }}", ask = FALSE) 57 | shell: Rscript {0} 58 | 59 | - name: Install matrixStats 60 | run: | 61 | install.packages("matrixStats") 62 | remotes::install_github("const-ae/MatrixGenerics", ref = "master") 63 | shell: Rscript {0} 64 | 65 | - name: Install system dependencies 66 | if: runner.os == 'Linux' 67 | env: 68 | RHUB_PLATFORM: linux-x86_64-ubuntu-gcc 69 | run: | 70 | Rscript -e "remotes::install_github('r-hub/sysreqs')" 71 | sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))") 72 | sudo -s eval "$sysreqs" 73 | sudo apt-get update && sudo apt-get -y install libcurl4-openssl-dev 74 | 75 | - name: Install further dependencies 76 | run: | 77 | install.packages(c("sessioninfo", "rcmdcheck")) 78 | BiocManager::install(c("BiocCheck", "BiocStyle")) 79 | install.packages(c("Rcpp", "rmarkdown", "testthat", "knitr", "bench", "rmarkdown")) 80 | shell: Rscript {0} 81 | 82 | - name: Session info 83 | run: | 84 | options(width = 100) 85 | pkgs <- installed.packages()[, "Package"] 86 | sessioninfo::session_info(pkgs, include_base = TRUE) 87 | shell: Rscript {0} 88 | 89 | - name: Check 90 | run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error", check_dir = "check") 91 | shell: Rscript {0} 92 | 93 | - name: BiocCheck 94 | run: | 95 | BiocCheck::BiocCheck(".") 96 | shell: Rscript {0} 97 | 98 | - name: Upload check results 99 | if: failure() 100 | uses: actions/upload-artifact@master 101 | with: 102 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 103 | path: check 104 | 105 | - name: Show testthat output 106 | if: always() 107 | run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true 108 | shell: bash 109 | 110 | - name: Upload check results 111 | if: failure() 112 | uses: actions/upload-artifact@master 113 | with: 114 | name: ${{ runner.os }}-r${{ matrix.config.r }}-bioc-${{ matrix.config.bioc }}-branch-release-results 115 | path: check 116 | 117 | - name: Test coverage 118 | if: matrix.config.os == 'macOS-latest' && matrix.config.r == 'devel' 119 | run: | 120 | install.packages("covr") 121 | covr::codecov(type = "all", token = "${{secrets.CODECOV_TOKEN}}") 122 | shell: Rscript {0} 123 | -------------------------------------------------------------------------------- /.github/workflows/R-CMD-check-devel.yaml: -------------------------------------------------------------------------------- 1 | on: 2 | push: 3 | branches: 4 | - develop 5 | pull_request: 6 | branches: 7 | - develop 8 | schedule: 9 | - cron: '0 8 * * 5' 10 | 11 | name: R-CMD-check-devel 12 | 13 | jobs: 14 | R-CMD-check-devel: 15 | if: "! contains(github.event.head_commit.message, '[ci skip]')" 16 | 17 | runs-on: ${{ matrix.config.os }} 18 | container: ${{ matrix.config.image }} 19 | 20 | name: ${{ matrix.config.os }} (${{ matrix.config.r }} - ${{ matrix.config.bioc }} - ${{ matrix.config.image }}) 21 | 22 | strategy: 23 | fail-fast: false 24 | matrix: 25 | config: 26 | - { os: windows-latest, r: '4.1 ', bioc: 'devel'} 27 | - { os: macOS-latest, r: '4.1', bioc: 'devel'} 28 | - { os: ubuntu-latest, r: '4.1', bioc: 'devel'} 29 | 30 | env: 31 | R_REMOTES_NO_ERRORS_FROM_WARNINGS: true 32 | CRAN: ${{ matrix.config.cran }} 33 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 34 | GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} 35 | 36 | steps: 37 | - name: Check out repo 38 | uses: actions/checkout@v2 39 | with: 40 | ref: develop 41 | 42 | - name: Set up R 43 | uses: r-lib/actions/setup-r@master 44 | with: 45 | r-version: ${{ matrix.config.r }} 46 | 47 | - name: Set up pandoc 48 | uses: r-lib/actions/setup-pandoc@master 49 | 50 | - name: Install remotes 51 | run: | 52 | install.packages('remotes') 53 | shell: Rscript {0} 54 | 55 | - name: Set BiocManager version 56 | run: | 57 | install.packages('BiocManager') 58 | BiocManager::install(version = "${{ matrix.config.bioc }}", ask = FALSE) 59 | shell: Rscript {0} 60 | 61 | - name: Install matrixStats 62 | if: matrix.config.matrixStats == 'devel' 63 | run: | 64 | remotes::install_github("HenrikBengtsson/matrixStats", ref = "develop") 65 | remotes::install_github("const-ae/MatrixGenerics", ref = "develop") 66 | shell: Rscript {0} 67 | 68 | - name: Install system dependencies 69 | if: runner.os == 'Linux' 70 | env: 71 | RHUB_PLATFORM: linux-x86_64-ubuntu-gcc 72 | run: | 73 | Rscript -e "remotes::install_github('r-hub/sysreqs')" 74 | sysreqs=$(Rscript -e "cat(sysreqs::sysreq_commands('DESCRIPTION'))") 75 | sudo -s eval "$sysreqs" 76 | sudo apt-get update && sudo apt-get -y install libcurl4-openssl-dev 77 | 78 | - name: Install further dependencies 79 | run: | 80 | install.packages(c("sessioninfo", "rcmdcheck")) 81 | BiocManager::install(c("BiocCheck", "BiocStyle")) 82 | install.packages(c("Rcpp", "rmarkdown", "testthat", "knitr", "bench", "rmarkdown")) 83 | shell: Rscript {0} 84 | 85 | - name: Session info 86 | run: | 87 | options(width = 100) 88 | pkgs <- installed.packages()[, "Package"] 89 | sessioninfo::session_info(pkgs, include_base = TRUE) 90 | shell: Rscript {0} 91 | 92 | - name: Check 93 | run: rcmdcheck::rcmdcheck(args = "--no-manual", error_on = "error", check_dir = "check") 94 | shell: Rscript {0} 95 | 96 | - name: BiocCheck 97 | run: | 98 | BiocCheck::BiocCheck(".") 99 | shell: Rscript {0} 100 | 101 | - name: Upload check results 102 | if: failure() 103 | uses: actions/upload-artifact@master 104 | with: 105 | name: ${{ runner.os }}-r${{ matrix.config.r }}-results 106 | path: check 107 | 108 | - name: Show testthat output 109 | if: always() 110 | run: find check -name 'testthat.Rout*' -exec cat '{}' \; || true 111 | shell: bash 112 | 113 | - name: Upload check results 114 | if: failure() 115 | uses: actions/upload-artifact@master 116 | with: 117 | name: ${{ runner.os }}-r${{ matrix.config.r }}-bioc-${{ matrix.config.bioc }}-branch-devel-results 118 | path: check 119 | 120 | - name: Test coverage 121 | if: matrix.config.os == 'macOS-latest' && matrix.config.r == 'devel' 122 | run: | 123 | install.packages("covr") 124 | covr::codecov(type = "all", token = "${{secrets.CODECOV_TOKEN}}") 125 | shell: Rscript {0} 126 | -------------------------------------------------------------------------------- /src/sample_rank.h: -------------------------------------------------------------------------------- 1 | #ifndef sample_rank_h 2 | #define sample_rank_h 3 | 4 | #include 5 | #include 6 | 7 | 8 | 9 | // This function was originally copied from https://stackoverflow.com/a/47619503/604854 10 | template 11 | std::vector calculate_sparse_rank(VT vec, IT positions, int number_of_zeros, 12 | std::string ties_method, std::string na_handling) { 13 | int vec_size = vec.size(); 14 | int total_size = vec_size + number_of_zeros; 15 | int original_number_of_zeros = number_of_zeros; 16 | std::vector result(total_size,0); 17 | //sorted index 18 | std::vector indx(vec_size); 19 | iota(indx.begin(),indx.end(),0); 20 | // sort small to large with NAs to the back 21 | sort(indx.begin(),indx.end(),[&vec](int i1, int i2){ 22 | if(Rcpp::NumericVector::is_na(vec[i1])) return false; 23 | if(Rcpp::NumericVector::is_na(vec[i2])) return true; 24 | return vec[i1] < vec[i2]; 25 | }); 26 | 27 | // rank observed values 28 | bool left_of_zero = vec_size > 0 && vec[indx[0]] < 0; 29 | int zero_start_rank = 1; 30 | for(int n, i=0;i < vec_size; i += n){ 31 | // This n stuff is for resolving ties. 32 | // https://stackoverflow.com/a/30827731/604854 33 | n = 1; 34 | while(i + n < vec_size && vec[indx[i]] == vec[indx[i + n]]){ 35 | ++n; 36 | } 37 | if(vec[indx[i]] == 0.0){ 38 | number_of_zeros += n; 39 | for(int k = 0; k < n; ++k){ 40 | if(ties_method == "average"){ 41 | result[positions[indx[i+k]]] = i + (number_of_zeros+1) / 2.0; 42 | }else if(ties_method == "min"){ 43 | result[positions[indx[i+k]]] = i + 1; 44 | }else if(ties_method == "max"){ 45 | result[positions[indx[i+k]]] = i + number_of_zeros; 46 | }else{ 47 | throw std::runtime_error("Unknown argument to ties_method: " + ties_method + ". Can only handle 'average', 'min', and 'max'."); 48 | } 49 | } 50 | }else if(!left_of_zero){ 51 | for(int k = 0; k < n; ++k){ 52 | if(ties_method == "average"){ 53 | result[positions[indx[i+k]]] = i+original_number_of_zeros + (n + 1)/2.0; 54 | }else if(ties_method == "min"){ 55 | result[positions[indx[i+k]]] = i+original_number_of_zeros + 1; 56 | }else if(ties_method == "max"){ 57 | result[positions[indx[i+k]]] = i+original_number_of_zeros + n; 58 | }else{ 59 | throw std::runtime_error("Unknown argument to ties_method: " + ties_method + ". Can only handle 'average', 'min', and 'max'."); 60 | } 61 | } 62 | } 63 | 64 | if(left_of_zero){ 65 | for(int k = 0; k < n; ++k){ 66 | if(ties_method == "average"){ 67 | result[positions[indx[i+k]]] = i+ (n + 1)/2.0; 68 | }else if(ties_method == "min"){ 69 | result[positions[indx[i+k]]] = i+ 1; 70 | }else if(ties_method == "max"){ 71 | result[positions[indx[i+k]]] = i+ n; 72 | }else{ 73 | throw std::runtime_error("Unknown argument to ties_method: " + ties_method + ". Can only handle 'average', 'min', and 'max'."); 74 | } 75 | } 76 | if(i + n == vec_size || vec[indx[i + n]] >= 0.0 || Rcpp::NumericVector::is_na(vec[indx[i + n]])){ 77 | left_of_zero = false; 78 | zero_start_rank = i + n + 1; 79 | } 80 | } 81 | } 82 | 83 | // Put the rank for each zero into result 84 | std::vector zero_pos(original_number_of_zeros); 85 | std::vector all_indices(total_size); 86 | iota(all_indices.begin(),all_indices.end(),0); 87 | std::set_difference(all_indices.begin(), all_indices.end(), positions.begin(), positions.end(), zero_pos.begin()); 88 | for(int zp: zero_pos){ 89 | if(ties_method == "average"){ 90 | result[zp] = (zero_start_rank * 2 - 1 + number_of_zeros) / 2.0; 91 | }else if(ties_method == "min"){ 92 | result[zp] = zero_start_rank; 93 | }else if(ties_method == "max"){ 94 | result[zp] = zero_start_rank + number_of_zeros - 1; 95 | }else{ 96 | throw std::runtime_error("Unknown argument to ties_method: " + ties_method + ". Can only handle 'average', 'min', and 'max'."); 97 | } 98 | } 99 | 100 | if(na_handling == "keep"){ 101 | // Put all NA's back into NA 102 | for(int i = 0; i < vec_size; ++i){ 103 | if(Rcpp::NumericVector::is_na(vec[i])){ 104 | if(std::is_same::value){ 105 | result[positions[i]] = NA_INTEGER; 106 | }else{ 107 | result[positions[i]] = NA_REAL; 108 | } 109 | } 110 | } 111 | } 112 | 113 | return result; 114 | } 115 | 116 | 117 | #endif /* sample_rank_h */ 118 | -------------------------------------------------------------------------------- /vignettes/sparseMatrixStats.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | title: "sparseMatrixStats" 3 | author: Constantin Ahlmann-Eltze 4 | date: "`r Sys.Date()`" 5 | output: BiocStyle::html_document 6 | vignette: > 7 | %\VignetteIndexEntry{sparseMatrixStats} 8 | %\VignetteEngine{knitr::rmarkdown} 9 | %\VignetteEncoding{UTF-8} 10 | --- 11 | 12 | ```{r, include = FALSE} 13 | knitr::opts_chunk$set( 14 | collapse = TRUE, 15 | comment = "#>" 16 | ) 17 | ``` 18 | 19 | # Installation 20 | 21 | You can install the release version of `r BiocStyle::Biocpkg("sparseMatrixStats")` from BioConductor: 22 | 23 | ``` r 24 | if (!requireNamespace("BiocManager", quietly = TRUE)) 25 | install.packages("BiocManager") 26 | 27 | BiocManager::install("sparseMatrixStats") 28 | ``` 29 | 30 | # Introduction 31 | 32 | The sparseMatrixStats package implements a number of summary functions for sparse matrices from the `r BiocStyle::CRANpkg("Matrix")` package. 33 | 34 | Let us load the package and create a synthetic sparse matrix. 35 | 36 | ```{r} 37 | library(sparseMatrixStats) 38 | # Matrix defines the sparse Matrix class 39 | # dgCMatrix that we will use 40 | library(Matrix) 41 | # For reproducibility 42 | set.seed(1) 43 | ``` 44 | 45 | Create a synthetic table with customers, items, and how often they bought that item. 46 | 47 | ```{r} 48 | customer_ids <- seq_len(100) 49 | item_ids <- seq_len(30) 50 | n_transactions <- 1000 51 | customer <- sample(customer_ids, size = n_transactions, replace = TRUE, 52 | prob = runif(100)) 53 | item <- sample(item_ids, size = n_transactions, replace = TRUE, 54 | prob = runif(30)) 55 | 56 | tmp <- table(paste0(customer, "-", item)) 57 | tmp2 <- strsplit(names(tmp), "-") 58 | purchase_table <- data.frame( 59 | customer = as.numeric(sapply(tmp2, function(x) x[1])), 60 | item = as.numeric(sapply(tmp2, function(x) x[2])), 61 | n = as.numeric(tmp) 62 | ) 63 | 64 | head(purchase_table, n = 10) 65 | ``` 66 | 67 | Let us turn the table into a matrix to simplify the analysis: 68 | 69 | ```{r} 70 | purchase_matrix <- sparseMatrix(purchase_table$customer, purchase_table$item, 71 | x = purchase_table$n, 72 | dims = c(100, 30), 73 | dimnames = list(customer = paste0("Customer_", customer_ids), 74 | item = paste0("Item_", item_ids))) 75 | purchase_matrix[1:10, 1:15] 76 | ``` 77 | 78 | We can see that some customers did not buy anything, where as 79 | some bought a lot. 80 | 81 | `sparseMatrixStats` can help us to identify interesting patterns in this data: 82 | 83 | 84 | ```{r} 85 | # How often was each item bough in total? 86 | colSums2(purchase_matrix) 87 | 88 | # What is the range of number of items each 89 | # customer bought? 90 | head(rowRanges(purchase_matrix)) 91 | 92 | # What is the variance in the number of items 93 | # each customer bought? 94 | head(rowVars(purchase_matrix)) 95 | 96 | # How many items did a customer not buy at all, one time, 2 times, 97 | # or exactly 4 times? 98 | head(rowTabulates(purchase_matrix, values = c(0, 1, 2, 4))) 99 | ``` 100 | 101 | 102 | ## Alternative Matrix Creation 103 | 104 | In the previous section, I demonstrated how to create a sparse matrix from scratch using the `sparseMatrix()` function. 105 | However, often you already have an existing matrix and want to convert it to a sparse representation. 106 | 107 | ```{r} 108 | mat <- matrix(0, nrow=10, ncol=6) 109 | mat[sample(seq_len(60), 4)] <- 1:4 110 | # Convert dense matrix to sparse matrix 111 | sparse_mat <- as(mat, "dgCMatrix") 112 | sparse_mat 113 | ``` 114 | 115 | The *sparseMatrixStats* package is a derivative of the `r BiocStyle::CRANpkg("matrixStats")` package and implements it's API for 116 | sparse matrices. For example, to calculate the variance for each column of `mat` you can do 117 | 118 | ```{r} 119 | apply(mat, 2, var) 120 | ``` 121 | 122 | However, this is quite inefficient and *matrixStats* provides the direct function 123 | 124 | ```{r} 125 | matrixStats::colVars(mat) 126 | ``` 127 | 128 | Now for sparse matrices, you can also just call 129 | 130 | ```{r} 131 | sparseMatrixStats::colVars(sparse_mat) 132 | ``` 133 | 134 | # Benchmark 135 | 136 | If you have a large matrix with many exact zeros, working on the sparse representation can considerably speed up the computations. 137 | 138 | I generate a dataset with 10,000 rows and 50 columns that is 99% empty 139 | 140 | ```{r} 141 | big_mat <- matrix(0, nrow=1e4, ncol=50) 142 | big_mat[sample(seq_len(1e4 * 50), 5000)] <- rnorm(5000) 143 | # Convert dense matrix to sparse matrix 144 | big_sparse_mat <- as(big_mat, "dgCMatrix") 145 | ``` 146 | 147 | I use the bench package to benchmark the performance difference: 148 | 149 | ```{r} 150 | bench::mark( 151 | sparseMatrixStats=sparseMatrixStats::colVars(big_sparse_mat), 152 | matrixStats=matrixStats::colVars(big_mat), 153 | apply=apply(big_mat, 2, var) 154 | ) 155 | ``` 156 | 157 | As you can see `sparseMatrixStats` is ca. 50 times fast than `matrixStats`, which in turn is 7 times faster than the `apply()` version. 158 | 159 | 160 | # Session Info 161 | 162 | ```{r} 163 | sessionInfo() 164 | ``` 165 | 166 | -------------------------------------------------------------------------------- /R/RcppExports.R: -------------------------------------------------------------------------------- 1 | # Generated by using Rcpp::compileAttributes() -> do not edit by hand 2 | # Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393 3 | 4 | dgCMatrix_colSums2 <- function(matrix, na_rm) { 5 | .Call('_sparseMatrixStats_dgCMatrix_colSums2', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 6 | } 7 | 8 | dgCMatrix_colMeans2 <- function(matrix, na_rm) { 9 | .Call('_sparseMatrixStats_dgCMatrix_colMeans2', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 10 | } 11 | 12 | dgCMatrix_colMedians <- function(matrix, na_rm) { 13 | .Call('_sparseMatrixStats_dgCMatrix_colMedians', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 14 | } 15 | 16 | dgCMatrix_colVars <- function(matrix, na_rm, center) { 17 | .Call('_sparseMatrixStats_dgCMatrix_colVars', PACKAGE = 'sparseMatrixStats', matrix, na_rm, center) 18 | } 19 | 20 | dgCMatrix_colMads <- function(matrix, na_rm, scale_factor, center) { 21 | .Call('_sparseMatrixStats_dgCMatrix_colMads', PACKAGE = 'sparseMatrixStats', matrix, na_rm, scale_factor, center) 22 | } 23 | 24 | dgCMatrix_colMins <- function(matrix, na_rm) { 25 | .Call('_sparseMatrixStats_dgCMatrix_colMins', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 26 | } 27 | 28 | dgCMatrix_colMaxs <- function(matrix, na_rm) { 29 | .Call('_sparseMatrixStats_dgCMatrix_colMaxs', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 30 | } 31 | 32 | dgCMatrix_colOrderStats <- function(matrix, which, na_rm) { 33 | .Call('_sparseMatrixStats_dgCMatrix_colOrderStats', PACKAGE = 'sparseMatrixStats', matrix, which, na_rm) 34 | } 35 | 36 | dgCMatrix_colLogSumExps <- function(matrix, na_rm) { 37 | .Call('_sparseMatrixStats_dgCMatrix_colLogSumExps', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 38 | } 39 | 40 | dgCMatrix_colProds <- function(matrix, na_rm) { 41 | .Call('_sparseMatrixStats_dgCMatrix_colProds', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 42 | } 43 | 44 | dgCMatrix_colWeightedMeans <- function(matrix, weights, na_rm) { 45 | .Call('_sparseMatrixStats_dgCMatrix_colWeightedMeans', PACKAGE = 'sparseMatrixStats', matrix, weights, na_rm) 46 | } 47 | 48 | dgCMatrix_colWeightedVars <- function(matrix, weights, na_rm) { 49 | .Call('_sparseMatrixStats_dgCMatrix_colWeightedVars', PACKAGE = 'sparseMatrixStats', matrix, weights, na_rm) 50 | } 51 | 52 | dgCMatrix_colCounts <- function(matrix, value, na_rm) { 53 | .Call('_sparseMatrixStats_dgCMatrix_colCounts', PACKAGE = 'sparseMatrixStats', matrix, value, na_rm) 54 | } 55 | 56 | dgCMatrix_colAnyNAs <- function(matrix) { 57 | .Call('_sparseMatrixStats_dgCMatrix_colAnyNAs', PACKAGE = 'sparseMatrixStats', matrix) 58 | } 59 | 60 | dgCMatrix_colAnys <- function(matrix, value, na_rm) { 61 | .Call('_sparseMatrixStats_dgCMatrix_colAnys', PACKAGE = 'sparseMatrixStats', matrix, value, na_rm) 62 | } 63 | 64 | dgCMatrix_colAlls <- function(matrix, value, na_rm) { 65 | .Call('_sparseMatrixStats_dgCMatrix_colAlls', PACKAGE = 'sparseMatrixStats', matrix, value, na_rm) 66 | } 67 | 68 | dgCMatrix_colQuantiles <- function(matrix, probs, na_rm) { 69 | .Call('_sparseMatrixStats_dgCMatrix_colQuantiles', PACKAGE = 'sparseMatrixStats', matrix, probs, na_rm) 70 | } 71 | 72 | dgCMatrix_colTabulate <- function(matrix, sorted_unique_values) { 73 | .Call('_sparseMatrixStats_dgCMatrix_colTabulate', PACKAGE = 'sparseMatrixStats', matrix, sorted_unique_values) 74 | } 75 | 76 | dgCMatrix_colCumsums <- function(matrix) { 77 | .Call('_sparseMatrixStats_dgCMatrix_colCumsums', PACKAGE = 'sparseMatrixStats', matrix) 78 | } 79 | 80 | dgCMatrix_colCumprods <- function(matrix) { 81 | .Call('_sparseMatrixStats_dgCMatrix_colCumprods', PACKAGE = 'sparseMatrixStats', matrix) 82 | } 83 | 84 | dgCMatrix_colCummins <- function(matrix) { 85 | .Call('_sparseMatrixStats_dgCMatrix_colCummins', PACKAGE = 'sparseMatrixStats', matrix) 86 | } 87 | 88 | dgCMatrix_colCummaxs <- function(matrix) { 89 | .Call('_sparseMatrixStats_dgCMatrix_colCummaxs', PACKAGE = 'sparseMatrixStats', matrix) 90 | } 91 | 92 | dgCMatrix_colRanks_num <- function(matrix, ties_method, na_handling, preserve_shape) { 93 | .Call('_sparseMatrixStats_dgCMatrix_colRanks_num', PACKAGE = 'sparseMatrixStats', matrix, ties_method, na_handling, preserve_shape) 94 | } 95 | 96 | dgCMatrix_colRanks_int <- function(matrix, ties_method, na_handling, preserve_shape) { 97 | .Call('_sparseMatrixStats_dgCMatrix_colRanks_int', PACKAGE = 'sparseMatrixStats', matrix, ties_method, na_handling, preserve_shape) 98 | } 99 | 100 | quantile_sparse <- function(values, number_of_zeros, prob) { 101 | .Call('_sparseMatrixStats_quantile_sparse', PACKAGE = 'sparseMatrixStats', values, number_of_zeros, prob) 102 | } 103 | 104 | dgCMatrix_rowSums2 <- function(matrix, na_rm) { 105 | .Call('_sparseMatrixStats_dgCMatrix_rowSums2', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 106 | } 107 | 108 | dgCMatrix_rowSums2_bool_col_select <- function(matrix, na_rm, col_selector) { 109 | .Call('_sparseMatrixStats_dgCMatrix_rowSums2_bool_col_select', PACKAGE = 'sparseMatrixStats', matrix, na_rm, col_selector) 110 | } 111 | 112 | dgCMatrix_rowSums2_int_col_select <- function(matrix, na_rm, col_selector) { 113 | .Call('_sparseMatrixStats_dgCMatrix_rowSums2_int_col_select', PACKAGE = 'sparseMatrixStats', matrix, na_rm, col_selector) 114 | } 115 | 116 | dgCMatrix_rowMeans2 <- function(matrix, na_rm) { 117 | .Call('_sparseMatrixStats_dgCMatrix_rowMeans2', PACKAGE = 'sparseMatrixStats', matrix, na_rm) 118 | } 119 | 120 | dgCMatrix_rowMeans2_bool_col_select <- function(matrix, na_rm, col_selector) { 121 | .Call('_sparseMatrixStats_dgCMatrix_rowMeans2_bool_col_select', PACKAGE = 'sparseMatrixStats', matrix, na_rm, col_selector) 122 | } 123 | 124 | dgCMatrix_rowMeans2_int_col_select <- function(matrix, na_rm, col_selector) { 125 | .Call('_sparseMatrixStats_dgCMatrix_rowMeans2_int_col_select', PACKAGE = 'sparseMatrixStats', matrix, na_rm, col_selector) 126 | } 127 | 128 | dgCMatrix_rowVars <- function(matrix, na_rm, center) { 129 | .Call('_sparseMatrixStats_dgCMatrix_rowVars', PACKAGE = 'sparseMatrixStats', matrix, na_rm, center) 130 | } 131 | 132 | -------------------------------------------------------------------------------- /tests/testthat/test-logicals.R: -------------------------------------------------------------------------------- 1 | set.seed(1) 2 | # source("~/prog/r_packages/sparseMatrixStats/tests/testthat/setup.R") 3 | mat <- make_matrix_with_all_features(nrow = 15, ncol=10) < 0 4 | sp_mat <- as(as(as(mat, "lMatrix"), "generalMatrix"), "CsparseMatrix") 5 | 6 | 7 | test_that("colSums works", { 8 | expect_equal(colSums2(sp_mat), matrixStats::colSums2(mat)) 9 | expect_equal(colSums2(sp_mat, na.rm=TRUE), matrixStats::colSums2(mat, na.rm=TRUE)) 10 | }) 11 | 12 | test_that("rowSums works", { 13 | sp_mat2 <- t(sp_mat) 14 | expect_equal(rowSums2(sp_mat2), matrixStats::colSums2(mat)) 15 | expect_equal(rowSums2(sp_mat2, na.rm=TRUE), matrixStats::colSums2(mat, na.rm=TRUE)) 16 | }) 17 | 18 | 19 | 20 | test_that("colMeans works", { 21 | expect_equal(colMeans2(sp_mat), matrixStats::colMeans2(mat)) 22 | expect_equal(colMeans2(sp_mat, na.rm=TRUE), matrixStats::colMeans2(mat, na.rm=TRUE)) 23 | }) 24 | 25 | test_that("rowMeans works", { 26 | sp_mat2 <- t(sp_mat) 27 | expect_equal(rowMeans2(sp_mat2), matrixStats::colMeans2(mat)) 28 | expect_equal(rowMeans2(sp_mat2, na.rm=TRUE), matrixStats::colMeans2(mat, na.rm=TRUE)) 29 | }) 30 | 31 | 32 | 33 | test_that("colCounts works", { 34 | expect_equal(colCounts(sp_mat, value=0), matrixStats::colCounts(mat, value=0)) 35 | expect_equal(colCounts(sp_mat, value = TRUE), matrixStats::colCounts(mat, value = TRUE)) 36 | expect_equal(colCounts(sp_mat, value = FALSE), matrixStats::colCounts(mat, value = FALSE)) 37 | expect_equal(colCounts(sp_mat, value = 42), matrixStats::colCounts(mat, value = 42)) 38 | expect_equal(colCounts(sp_mat, na.rm=TRUE, value = 0), matrixStats::colCounts(mat, na.rm=TRUE, value = 0)) 39 | expect_equal(colCounts(sp_mat, na.rm=TRUE, value = 42), matrixStats::colCounts(mat, na.rm=TRUE, value = 42)) 40 | }) 41 | 42 | 43 | test_that("colAnyNAs works", { 44 | expect_equal(colAnyNAs(sp_mat), matrixStats::colAnyNAs(mat)) 45 | }) 46 | 47 | 48 | test_that("colAnys works", { 49 | expect_equal(colAnys(sp_mat), matrixStats::colAnys(mat)) 50 | expect_equal(colAnys(sp_mat, na.rm=TRUE), matrixStats::colAnys(mat, na.rm=TRUE)) 51 | expect_equal(colAnys(sp_mat, value = FALSE), matrixStats::colAnys(mat, value = FALSE)) 52 | expect_equal(colAnys(sp_mat, na.rm=TRUE, value = FALSE), matrixStats::colAnys(mat, na.rm=TRUE, value = FALSE)) 53 | expect_equal(colAnys(sp_mat, value=0), matrixStats::colAnys(mat, value=0)) 54 | expect_equal(colAnys(sp_mat, na.rm=TRUE, value=0), matrixStats::colAnys(mat, na.rm=TRUE, value = 0)) 55 | expect_equal(colAnys(sp_mat, value = 42), matrixStats::colAnys(mat, value = 42)) 56 | expect_equal(colAnys(sp_mat, na.rm=TRUE, value = 42), matrixStats::colAnys(mat, na.rm=TRUE, value = 42)) 57 | }) 58 | 59 | 60 | test_that("colAlls works", { 61 | expect_equal(colAlls(sp_mat), matrixStats::colAlls(mat)) 62 | expect_equal(colAlls(sp_mat, na.rm=TRUE), matrixStats::colAlls(mat, na.rm=TRUE)) 63 | # expect_equal(colAlls(sp_mat, value = FALSE), matrixStats::colAlls(array(as.logical(mat), dim = dim(mat)), value = FALSE)) 64 | expect_equal(colAlls(sp_mat, na.rm=TRUE, value = FALSE), matrixStats::colAlls(array(as.logical(mat), dim = dim(mat)), na.rm=TRUE, value = FALSE)) 65 | expect_equal(colAlls(sp_mat, value=0), matrixStats::colAlls(mat, value=0)) 66 | expect_equal(colAlls(sp_mat, na.rm=TRUE, value=0), matrixStats::colAlls(mat, na.rm=TRUE, value = 0)) 67 | expect_equal(colAlls(sp_mat, value = 42), matrixStats::colAlls(mat, value = 42)) 68 | expect_equal(colAlls(sp_mat, na.rm=TRUE, value = 42), matrixStats::colAlls(mat, na.rm=TRUE, value = 42)) 69 | }) 70 | 71 | 72 | 73 | test_that("colLogSumExps works", { 74 | expect_equal(colLogSumExps(sp_mat), matrixStats::colLogSumExps(mat)) 75 | expect_equal(colLogSumExps(sp_mat, na.rm=TRUE), matrixStats::colLogSumExps(mat, na.rm=TRUE)) 76 | }) 77 | 78 | 79 | test_that("colProds works", { 80 | expect_equal(colProds(sp_mat), matrixStats::colProds(mat)) 81 | expect_equal(colProds(sp_mat, na.rm=TRUE), matrixStats::colProds(mat, na.rm=TRUE)) 82 | }) 83 | 84 | test_that("colQuantiles works", { 85 | expect_equal(colQuantiles(sp_mat), matrixStats::colQuantiles(mat)) 86 | expect_equal(colQuantiles(sp_mat, na.rm=TRUE), matrixStats::colQuantiles(mat, na.rm=TRUE)) 87 | 88 | skip("With R 4.3 (?) 'colQuantiles' on logical data suddenly produces an error. I am unsure why.") 89 | expect_equal(colQuantiles(sp_mat, type = 1), matrixStats::colQuantiles(mat, type = 1)) 90 | expect_equal(colQuantiles(sp_mat, type = 1, prob = 0.5), matrixStats::colQuantiles(mat, type = 1, prob = 0.5)) 91 | }) 92 | 93 | 94 | 95 | test_that("colTabulates works", { 96 | expect_equal(colTabulates(sp_mat), matrixStats::colTabulates(mat)) 97 | expect_equal(colTabulates(sp_mat, values = integer(0L)), matrixStats::colTabulates(mat, values = integer(0L))) 98 | values <- c(FALSE, TRUE, TRUE, NA) 99 | expect_equal(colTabulates(sp_mat, values = values), matrixStats::colTabulates(mat, values = values)) 100 | expect_equal(colTabulates(sp_mat, values = c(TRUE, values)), matrixStats::colTabulates(mat, values = c(TRUE, values))) 101 | expect_equal(colTabulates(sp_mat, values = values[-1]), matrixStats::colTabulates(mat, values = values[-1])) 102 | skip("matrixStats doesn't convert values to logical if mat is logical?!") 103 | expect_equal(colTabulates(sp_mat, values = c(1, values)), matrixStats::colTabulates(mat, values = c(1, values))) 104 | }) 105 | 106 | 107 | test_that("cumulative functions work", { 108 | expect_equal(colCumsums(sp_mat), matrixStats::colCumsums(mat)) 109 | }) 110 | 111 | 112 | test_that("colIQRs works", { 113 | expect_equal(colIQRs(sp_mat), matrixStats::colIQRs(mat)) 114 | }) 115 | 116 | 117 | test_that("colWeightedMeans works", { 118 | weights <- rnorm(nrow(sp_mat), mean=4, sd=0.1) 119 | expect_equal(colWeightedMeans(sp_mat, w=weights), matrixStats::colWeightedMeans(mat, w=weights)) 120 | expect_equal(colWeightedMeans(sp_mat, na.rm=TRUE, w=weights), matrixStats::colWeightedMeans(mat, na.rm=TRUE, w=weights)) 121 | }) 122 | 123 | 124 | test_that("colWeightedVars works", { 125 | weights <- rnorm(nrow(sp_mat), mean=4, sd=0.1) 126 | expect_equal(colWeightedVars(sp_mat, w=weights), matrixStats::colWeightedVars(mat, w=weights)) 127 | expect_equal(colWeightedVars(sp_mat, na.rm=TRUE), matrixStats::colWeightedVars(mat, na.rm=TRUE)) 128 | }) 129 | 130 | 131 | test_that("colWeightedSds works", { 132 | weights <- rnorm(nrow(sp_mat), mean=4, sd=0.1) 133 | expect_equal(colWeightedSds(sp_mat, w=weights), matrixStats::colWeightedSds(mat, w=weights)) 134 | expect_equal(colWeightedSds(sp_mat, na.rm=TRUE), matrixStats::colWeightedSds(mat, na.rm=TRUE)) 135 | }) 136 | 137 | 138 | test_that("colCollapse works", { 139 | expect_equal(colCollapse(sp_mat, idxs = 1), matrixStats::colCollapse(mat, idxs = 1)) 140 | expect_equal(colCollapse(sp_mat, idxs = c(1,3)), matrixStats::colCollapse(mat, idxs = c(1,3))) 141 | expect_equal(colCollapse(sp_mat, idxs = 1:5, cols = min(ncol(mat), 3)), matrixStats::colCollapse(mat, idxs = 1:5, cols = min(ncol(mat), 3))) 142 | }) 143 | 144 | test_that("colAvgsPerRowSet works", { 145 | S <- suppressWarnings(matrix(seq_len(nrow(mat)), ncol = 2)) 146 | expect_equal(colAvgsPerRowSet(sp_mat, S = S, na.rm = TRUE), matrixStats::colAvgsPerRowSet(mat, S = S)) 147 | }) 148 | -------------------------------------------------------------------------------- /src/row_methods.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "SparseMatrixView.h" 3 | #include "ColumnView.h" 4 | #include "VectorSubsetView.h" 5 | #include "SkipNAVectorSubsetView.h" 6 | #include "types.h" 7 | 8 | using namespace Rcpp; 9 | 10 | 11 | 12 | // [[Rcpp::export]] 13 | NumericVector dgCMatrix_rowSums2(S4 matrix, bool na_rm){ 14 | IntegerVector dim = matrix.slot("Dim"); 15 | NumericVector values = matrix.slot("x"); 16 | IntegerVector row_indices = matrix.slot("i"); 17 | std::vector result (dim[0], 0.0); 18 | 19 | double* val_iter = values.begin(); 20 | int* idx_iter = row_indices.begin(); 21 | while(val_iter != values.end() && idx_iter != row_indices.end()){ 22 | if(na_rm && ISNA(*val_iter)){ 23 | // Do nothing 24 | }else{ 25 | result[*idx_iter] += *val_iter; 26 | } 27 | ++val_iter; 28 | ++idx_iter; 29 | } 30 | return wrap(result); 31 | } 32 | 33 | // [[Rcpp::export]] 34 | NumericVector dgCMatrix_rowSums2_bool_col_select(S4 matrix, bool na_rm, LogicalVector col_selector){ 35 | IntegerVector dim = matrix.slot("Dim"); 36 | NumericVector values = matrix.slot("x"); 37 | IntegerVector row_indices = matrix.slot("i"); 38 | IntegerVector pointers = matrix.slot("p"); 39 | std::vector result (dim[0], 0.0); 40 | int n_cols = dim[1]; 41 | 42 | for(int i = 0; i < n_cols; ++i){ 43 | if(col_selector[i] == 1){ 44 | int val_idx = pointers[i]; 45 | int end_idx = pointers[i + 1]; 46 | for(;val_idx < end_idx; ++val_idx){ 47 | if(ISNA(values[val_idx]) && na_rm){ 48 | // Do nothing 49 | }else{ 50 | result[row_indices[val_idx]] += values[val_idx]; 51 | } 52 | } 53 | }else if(col_selector[i] == NA_LOGICAL && ! na_rm){ 54 | for(int r = 0; r < dim[0]; ++r){ 55 | result[r] = NA_REAL; 56 | } 57 | break; 58 | }else if(col_selector[i] == NA_LOGICAL && na_rm){ 59 | // Do nothing 60 | } 61 | } 62 | 63 | return wrap(result); 64 | } 65 | 66 | // [[Rcpp::export]] 67 | NumericVector dgCMatrix_rowSums2_int_col_select(S4 matrix, bool na_rm, IntegerVector col_selector){ 68 | IntegerVector dim = matrix.slot("Dim"); 69 | NumericVector values = matrix.slot("x"); 70 | IntegerVector row_indices = matrix.slot("i"); 71 | IntegerVector pointers = matrix.slot("p"); 72 | std::vector result (dim[0], 0.0); 73 | int n_cols = dim[1]; 74 | 75 | for(int i : col_selector){ 76 | if(i == NA_INTEGER && ! na_rm){ 77 | for(int r = 0; r < dim[0]; ++r){ 78 | result[r] = NA_REAL; 79 | } 80 | break; 81 | }else if(i == NA_INTEGER && na_rm){ 82 | // Do nothing 83 | }else if(i < 1 || i > n_cols){ 84 | Rcpp::stop("The values in cols must be between 1 and ncol: " + std::to_string(i)); 85 | }else{ 86 | i -= 1; 87 | int val_idx = pointers[i]; 88 | int end_idx = pointers[i + 1]; 89 | for(;val_idx < end_idx; ++val_idx){ 90 | if(ISNA(values[val_idx]) && na_rm){ 91 | // Do nothing 92 | }else{ 93 | result[row_indices[val_idx]] += values[val_idx]; 94 | } 95 | } 96 | } 97 | } 98 | 99 | return wrap(result); 100 | } 101 | 102 | 103 | 104 | // [[Rcpp::export]] 105 | NumericVector dgCMatrix_rowMeans2(S4 matrix, bool na_rm){ 106 | IntegerVector dim = matrix.slot("Dim"); 107 | NumericVector values = matrix.slot("x"); 108 | IntegerVector row_indices = matrix.slot("i"); 109 | std::vector result (dim[0], 0.0); 110 | std::vector nas_per_row (dim[0], 0); 111 | 112 | double* val_iter = values.begin(); 113 | int* idx_iter = row_indices.begin(); 114 | auto val_end = values.end(); 115 | auto row_end = row_indices.end(); 116 | while(val_iter != val_end && idx_iter != row_end){ 117 | if(na_rm && ISNA(*val_iter)){ 118 | nas_per_row[*idx_iter] += 1; 119 | }else{ 120 | result[*idx_iter] += *val_iter; 121 | } 122 | ++val_iter; 123 | ++idx_iter; 124 | } 125 | auto res_iter = result.begin(); 126 | auto res_end = result.end(); 127 | auto na_count_iter = nas_per_row.begin(); 128 | auto na_count_end = nas_per_row.end(); 129 | while(res_iter != res_end && na_count_iter != na_count_end){ 130 | *res_iter = *res_iter / (dim[1] - *na_count_iter); 131 | ++res_iter; 132 | ++na_count_iter; 133 | } 134 | 135 | return wrap(result); 136 | } 137 | 138 | 139 | // [[Rcpp::export]] 140 | NumericVector dgCMatrix_rowMeans2_bool_col_select(S4 matrix, bool na_rm, LogicalVector col_selector){ 141 | IntegerVector dim = matrix.slot("Dim"); 142 | NumericVector values = matrix.slot("x"); 143 | IntegerVector row_indices = matrix.slot("i"); 144 | IntegerVector pointers = matrix.slot("p"); 145 | std::vector result (dim[0], 0.0); 146 | std::vector nas_per_row (dim[0], 0); 147 | int n_cols = dim[1]; 148 | 149 | for(int i = 0; i < n_cols; ++i){ 150 | if(col_selector[i] == 1){ 151 | int val_idx = pointers[i]; 152 | int end_idx = pointers[i + 1]; 153 | for(;val_idx < end_idx; ++val_idx){ 154 | if(ISNA(values[val_idx]) && na_rm){ 155 | nas_per_row[row_indices[val_idx]] += 1; 156 | }else{ 157 | result[row_indices[val_idx]] += values[val_idx]; 158 | } 159 | } 160 | }else if(col_selector[i] == NA_LOGICAL && ! na_rm){ 161 | for(int r = 0; r < dim[0]; ++r){ 162 | result[r] = NA_REAL; 163 | } 164 | break; 165 | }else if(col_selector[i] == NA_LOGICAL && na_rm){ 166 | for(int r = 0; r < dim[0]; ++r){ 167 | nas_per_row[r] += 1; 168 | } 169 | } 170 | } 171 | 172 | 173 | auto res_iter = result.begin(); 174 | auto res_end = result.end(); 175 | auto na_count_iter = nas_per_row.begin(); 176 | auto na_count_end = nas_per_row.end(); 177 | int n_eff_cols = 0; 178 | for(int c : col_selector){ 179 | n_eff_cols += (c == 1); 180 | } 181 | while(res_iter != res_end && na_count_iter != na_count_end){ 182 | *res_iter = *res_iter / (n_eff_cols - *na_count_iter); 183 | ++res_iter; 184 | ++na_count_iter; 185 | } 186 | 187 | return wrap(result); 188 | } 189 | 190 | 191 | // [[Rcpp::export]] 192 | NumericVector dgCMatrix_rowMeans2_int_col_select(S4 matrix, bool na_rm, IntegerVector col_selector){ 193 | IntegerVector dim = matrix.slot("Dim"); 194 | NumericVector values = matrix.slot("x"); 195 | IntegerVector row_indices = matrix.slot("i"); 196 | IntegerVector pointers = matrix.slot("p"); 197 | std::vector result (dim[0], 0.0); 198 | std::vector nas_per_row (dim[0], 0); 199 | int n_cols = dim[1]; 200 | 201 | // This for loop is a modified version of dgCMatrix_rowSums2_int_col_select 202 | for(int i : col_selector){ 203 | if(! na_rm && i == NA_INTEGER){ 204 | for(int r = 0; r < dim[0]; ++r){ 205 | result[r] = NA_REAL; 206 | } 207 | break; 208 | }else if(na_rm && i == NA_INTEGER){ 209 | for(int r = 0; r < dim[0]; ++r){ 210 | nas_per_row[r] += 1; 211 | } 212 | }else if(i < 1 || i > n_cols){ 213 | Rcpp::stop("The values in cols must be between 1 and ncol: " + std::to_string(i)); 214 | }else{ 215 | i -= 1; 216 | int val_idx = pointers[i]; 217 | int end_idx = pointers[i + 1]; 218 | for(;val_idx < end_idx; ++val_idx){ 219 | if(na_rm && ISNA(values[val_idx])){ 220 | nas_per_row[row_indices[val_idx]] += 1; 221 | }else{ 222 | result[row_indices[val_idx]] += values[val_idx]; 223 | } 224 | } 225 | } 226 | } 227 | 228 | 229 | auto res_iter = result.begin(); 230 | auto res_end = result.end(); 231 | auto na_count_iter = nas_per_row.begin(); 232 | auto na_count_end = nas_per_row.end(); 233 | int n_eff_cols = col_selector.length(); 234 | while(res_iter != res_end && na_count_iter != na_count_end){ 235 | *res_iter = *res_iter / (n_eff_cols - *na_count_iter); 236 | ++res_iter; 237 | ++na_count_iter; 238 | } 239 | 240 | return wrap(result); 241 | } 242 | 243 | 244 | 245 | 246 | 247 | // [[Rcpp::export]] 248 | NumericVector dgCMatrix_rowVars(S4 matrix, bool na_rm, Nullable center){ 249 | bool center_provided = center.isNotNull(); 250 | NumericVector means(0); 251 | if(center_provided){ 252 | means = Rcpp::as(center.get()); 253 | }else{ 254 | means = dgCMatrix_rowMeans2(matrix, na_rm); 255 | } 256 | IntegerVector dim = matrix.slot("Dim"); 257 | NumericVector values = matrix.slot("x"); 258 | IntegerVector row_indices = matrix.slot("i"); 259 | std::vector result (dim[0], 0.0); 260 | std::vector nas_per_row (dim[0], 0); 261 | std::vector zeros_per_row (dim[0], dim[1]); 262 | 263 | double* val_iter = values.begin(); 264 | auto val_end = values.end(); 265 | int* idx_iter = row_indices.begin(); 266 | auto idx_end = row_indices.end(); 267 | while(val_iter != val_end && idx_iter != idx_end){ 268 | if(na_rm && ISNA(*val_iter)){ 269 | nas_per_row[*idx_iter] += 1; 270 | }else{ 271 | LDOUBLE diff = (*val_iter - means[*idx_iter]); 272 | result[*idx_iter] += diff * diff; 273 | } 274 | zeros_per_row[*idx_iter] -= 1; 275 | ++val_iter; 276 | ++idx_iter; 277 | } 278 | auto res_iter = result.begin(); 279 | auto res_end = result.end(); 280 | auto na_count_iter = nas_per_row.begin(); 281 | auto na_count_end = nas_per_row.end(); 282 | auto zero_count_iter = zeros_per_row.begin(); 283 | auto zero_count_end = zeros_per_row.end(); 284 | auto mean_iter = means.begin(); 285 | auto mean_end = means.end(); 286 | while(res_iter != res_end && 287 | na_count_iter != na_count_end && 288 | zero_count_iter != zero_count_end && 289 | mean_iter != mean_end){ 290 | if(dim[1] - *na_count_iter - 1 < 0){ 291 | *res_iter = R_NaN; 292 | }else{ 293 | *res_iter = ((*res_iter) + (*zero_count_iter) * (*mean_iter) * (*mean_iter)) / (dim[1] - *na_count_iter - 1); 294 | } 295 | ++res_iter; 296 | ++na_count_iter; 297 | ++zero_count_iter; 298 | ++mean_iter; 299 | } 300 | return wrap(result); 301 | } 302 | 303 | 304 | 305 | 306 | 307 | -------------------------------------------------------------------------------- /README.Rmd: -------------------------------------------------------------------------------- 1 | --- 2 | output: github_document 3 | --- 4 | 5 | 6 | 7 | ```{r, include = FALSE} 8 | knitr::opts_chunk$set( 9 | collapse = TRUE, 10 | comment = "#>", 11 | fig.path = "man/figures/README-", 12 | out.width = "100%" 13 | ) 14 | ``` 15 | 16 | # sparseMatrixStats 17 | 18 | 19 | [![codecov](https://codecov.io/gh/const-ae/sparseMatrixStats/branch/master/graph/badge.svg)](https://codecov.io/gh/const-ae/sparseMatrixStats) 20 | 21 | 22 | 23 | The goal of `sparseMatrixStats` is to make the API of [matrixStats](https://github.com/HenrikBengtsson/matrixStats) available 24 | for sparse matrices. 25 | 26 | ## Installation 27 | 28 | You can install the release version of *[sparseMatrixStats](https://bioconductor.org/packages/sparseMatrixStats)* from BioConductor: 29 | 30 | ``` r 31 | if (!requireNamespace("BiocManager", quietly = TRUE)) 32 | install.packages("BiocManager") 33 | 34 | BiocManager::install("sparseMatrixStats") 35 | ``` 36 | 37 | Alternatively, you can get the development version of the package from [GitHub](https://github.com/const-ae/sparseMatrixStats) with: 38 | 39 | ``` r 40 | # install.packages("devtools") 41 | devtools::install_github("const-ae/sparseMatrixStats") 42 | ``` 43 | 44 | If you have trouble with the installation, see the end of the README. 45 | 46 | ## Example 47 | 48 | ```{r include=FALSE} 49 | set.seed(1) 50 | ``` 51 | 52 | 53 | ```{r} 54 | library(sparseMatrixStats) 55 | ``` 56 | 57 | 58 | ```{r} 59 | mat <- matrix(0, nrow=10, ncol=6) 60 | mat[sample(seq_len(60), 4)] <- 1:4 61 | # Convert dense matrix to sparse matrix 62 | sparse_mat <- as(mat, "dgCMatrix") 63 | sparse_mat 64 | ``` 65 | 66 | The package provides an interface to quickly do common operations on the rows or columns. For example calculate 67 | the variance: 68 | 69 | ```{r} 70 | apply(mat, 2, var) 71 | matrixStats::colVars(mat) 72 | sparseMatrixStats::colVars(sparse_mat) 73 | ``` 74 | 75 | On this small example data, all methods are basically equally fast, but if we have a much larger dataset, the 76 | optimizations for the sparse data start to show. 77 | 78 | I generate a dataset with 10,000 rows and 50 columns that is 99% empty 79 | 80 | ```{r} 81 | big_mat <- matrix(0, nrow=1e4, ncol=50) 82 | big_mat[sample(seq_len(1e4 * 50), 5000)] <- rnorm(5000) 83 | # Convert dense matrix to sparse matrix 84 | big_sparse_mat <- as(big_mat, "dgCMatrix") 85 | ``` 86 | 87 | I use the `bench` package to benchmark the performance difference: 88 | 89 | ```{r} 90 | bench::mark( 91 | sparseMatrixStats=sparseMatrixStats::colVars(big_sparse_mat), 92 | matrixStats=matrixStats::colVars(big_mat), 93 | apply=apply(big_mat, 2, var) 94 | ) 95 | ``` 96 | 97 | 98 | As you can see `sparseMatrixStats` is ca. 35 times fast than `matrixStats`, which in turn is 7 times faster than the `apply()` version. 99 | 100 | 101 | 102 | 103 | 104 | # API 105 | 106 | The package now supports all functions from the `matrixStats` API for column sparse matrices (`dgCMatrix`). And thanks to the [`MatrixGenerics`](https://bioconductor.org/packages/MatrixGenerics/) it can be easily integrated along-side [`matrixStats`](https://cran.r-project.org/package=matrixStats) and [`DelayedMatrixStats`](https://bioconductor.org/packages/DelayedMatrixStats/). 107 | Note that the `rowXXX()` functions are called by transposing the input and calling the corresponding `colXXX()` function. Special optimized implementations are available for `rowSums2()`, `rowMeans2()`, and `rowVars()`. 108 | 109 | ```{r, echo=FALSE, results="asis"} 110 | matrixStats_functions <- sort( 111 | c("colsum", "rowsum", grep("^(col|row)", 112 | getNamespaceExports("matrixStats"), 113 | value = TRUE))) 114 | DelayedMatrixStats_functions <- grep("^(col|row)", getNamespaceExports("DelayedMatrixStats"), value=TRUE) 115 | DelayedArray_functions <- grep("^(col|row)", getNamespaceExports("DelayedArray"), value=TRUE) 116 | sparseMatrixStats_functions <- grep("^(col|row)", getNamespaceExports("sparseMatrixStats"), value=TRUE) 117 | 118 | notes <- c("colAnyMissings"="Not implemented because it is deprecated in favor of `colAnyNAs()`", 119 | "rowAnyMissings"="Not implemented because it is deprecated in favor of `rowAnyNAs()`", 120 | "colsum"="Base R function", 121 | "rowsum"="Base R function", 122 | "colWeightedMedians"="Only equivalent if `interpolate=FALSE`", 123 | "rowWeightedMedians"="Only equivalent if `interpolate=FALSE`", 124 | "colWeightedMads"="Sparse version behaves slightly differently, because it always uses `interpolate=FALSE`.", 125 | "rowWeightedMads"="Sparse version behaves slightly differently, because it always uses `interpolate=FALSE`.") 126 | 127 | api_df <- data.frame( 128 | Method = paste0(matrixStats_functions, "()"), 129 | matrixStats = ifelse(matrixStats_functions %in% matrixStats_functions, "✔", "❌"), 130 | sparseMatrixStats = ifelse(matrixStats_functions %in%sparseMatrixStats_functions, "✔", "❌"), 131 | Notes = ifelse(matrixStats_functions %in% names(notes), notes[matrixStats_functions], ""), 132 | stringsAsFactors = FALSE 133 | ) 134 | knitr::kable(api_df, row.names = FALSE) 135 | 136 | ``` 137 | 138 | 139 | 140 | # Installation Problems 141 | 142 | `sparseMatrixStats` uses features from C++14 and as the standard is more than 6 years old, I thought this wouldn't cause problems. In most circumstances this is true, but there are reoccuring reports, that the installation fails for some people and that is of course annoying. The typical error message is: 143 | ``` 144 | Error: C++14 standard requested but CXX14 is not defined 145 | ``` 146 | 147 | The main reason that the installation fails is that the compiler is too old. Sufficient support for C++14 came in 148 | 149 | * `clang` version 3.4 150 | * `gcc` version 4.9 151 | 152 | Accordingly, you must have a compiler available that is at least that new. If you run on the command line 153 | 154 | ```{bash eval=FALSE, include=TRUE} 155 | $ gcc --version 156 | ``` 157 | 158 | and it says 4.8, you will have to install a newer compiler. At the end of the section, I have collected a few tips to install an appropriate version on different distributions. 159 | 160 | If you have recent version of `gcc` (>=4.9) or `clang` (>= 3.4) installed, but you still see the error message 161 | ``` 162 | Error: C++14 standard requested but CXX14 is not defined 163 | ``` 164 | the problem is that R doesn't yet know about it. 165 | 166 | The solution is to either create a `~/.R/Makevars` file and define 167 | ``` 168 | CXX14 = g++ 169 | CXX14FLAGS = -g -O2 $(LTO) 170 | CXX14PICFLAGS = -fpic 171 | CXX14STD = -std=gnu++14 172 | ``` 173 | or simply call 174 | ```{r eval=FALSE, include=TRUE} 175 | withr::with_makevars( 176 | new = c(CXX14 = "g++", CXX14FLAGS = "-g -O2 $(LTO)", 177 | CXX14PICFLAGS = "-fpic", CXX14STD = "-std=gnu++14"), 178 | code = { 179 | BiocManager::install("sparseMatrixStats") 180 | }) 181 | ``` 182 | 183 | 184 | 185 | ### Update Compiler 186 | 187 | 188 | #### CentOS / Scientic Linux / RHEL 189 | 190 | One of the main culprits causing trouble is CentOS 7. It is popular in scientific computing and is still supported until 2024. It does, however, by default come with a very old version of `gcc` (4.8.5). 191 | 192 | To install a more recent compiler, we can use [devtoolset](https://www.softwarecollections.org/en/scls/rhscl/devtoolset-7/). First, we enable the Software Collection Tools and then install for example `gcc` version 7: 193 | 194 | ```{bash eval=FALSE} 195 | $ yum install centos-release-scl 196 | $ yum install devtoolset-7-gcc* 197 | ``` 198 | 199 | We can now either activate the new compiler for an R session 200 | ```{bash eval=FALSE} 201 | $ scl enable devtoolset-7 R 202 | ``` 203 | and then call 204 | ```{r eval=FALSE, include=TRUE} 205 | withr::with_makevars( 206 | new = c(CXX14 = "g++", CXX14FLAGS = "-g -O2 $(LTO)", 207 | CXX14PICFLAGS = "-fpic", CXX14STD = "-std=gnu++14"), 208 | code = { 209 | BiocManager::install("sparseMatrixStats") 210 | }) 211 | ``` 212 | 213 | or we refer to the full path of the newly installed g++ from a standard R session 214 | 215 | ```{r eval=FALSE, include=TRUE} 216 | withr::with_makevars( 217 | new = c(CXX14 = "/opt/rh/devtoolset-7/root/usr/bin/g++", 218 | CXX14FLAGS = "-g -O2 $(LTO)", CXX14PICFLAGS = "-fpic", 219 | CXX14STD = "-std=gnu++14"), 220 | code = { 221 | BiocManager::install("sparseMatrixStats") 222 | }) 223 | ``` 224 | 225 | Note, that our shenanigans are only necessary once, when we install `sparseMatrixStats`. After the successful installation of the package, we can use R as usual. 226 | 227 | 228 | #### Debian 229 | 230 | All Debian releases later than Jessie (i.e. Stretch, Buster, Bullseye) are recent enough and should install sparseMatrixStats without problems. 231 | 232 | I was able to install `sparseMatrixStats` on Debian Jessie (which comes with `gcc` version 4.9.2) by providing the necessary Makefile arguments 233 | ```{r eval=FALSE, include=TRUE} 234 | withr::with_makevars( 235 | new = c(CXX14 = "g++", 236 | CXX14FLAGS = "-g -O2 $(LTO)", CXX14PICFLAGS = "-fpic", 237 | CXX14STD = "-std=gnu++14"), 238 | code = { 239 | BiocManager::install("sparseMatrixStats") 240 | }) 241 | ``` 242 | 243 | Debian Wheezy comes with `gcc` 4.7, which does not support C++14. On the other hand, the last R release that was backported to Wheezy is 3.2.5 (see information on [CRAN](https://cloud.r-project.org/bin/linux/debian/#debian-wheezy-oldoldoldstable)). Thus, if you are still on Wheezy, I would encourage you to update your OS. 244 | 245 | 246 | #### Ubuntu 247 | 248 | Since 16.04, Ubuntu comes with a recent enough compiler. 249 | 250 | Ubuntu 14.04 comes with `gcc` 4.8.5, but updating to `gcc-5` is easy: 251 | 252 | ```{bash eval=FALSE, include=TRUE} 253 | $ sudo add-apt-repository ppa:ubuntu-toolchain-r/test 254 | $ sudo apt-get update 255 | $ sudo apt-get install gcc-5 g++-5 256 | ``` 257 | After that, you can install `sparseMatrixStats` with a custom Makevars variables that refer to the new compiler 258 | ```{r eval=FALSE, include=TRUE} 259 | withr::with_makevars( 260 | new = c(CXX14 = "g++-5", 261 | CXX14FLAGS = "-g -O2 $(LTO)", CXX14PICFLAGS = "-fpic", 262 | CXX14STD = "-std=gnu++14"), 263 | code = { 264 | BiocManager::install("sparseMatrixStats") 265 | }) 266 | ``` 267 | 268 | #### MacOS 269 | 270 | No trouble reported so far. Just do: 271 | 272 | ```{r eval=FALSE, include=TRUE} 273 | BiocManager::install("sparseMatrixStats") 274 | ``` 275 | 276 | #### Windows 277 | 278 | It is important that you have [RTools40](https://cran.r-project.org/bin/windows/Rtools/) installed. After that, you shouldn't have any troubles installing `sparseMatrixStats` directly from Bioconductor: 279 | 280 | ```{r eval=FALSE, include=TRUE} 281 | BiocManager::install("sparseMatrixStats") 282 | ``` 283 | 284 | 285 | ### But I still have a problems 286 | 287 | 1. *Please* make sure to carefully read the full problem section. 288 | 2. Make sure that you are using at least R 4.0.0. 289 | 3. Make sure your compiler is new enough to support C++14 (ie. `gcc` >= 4.9 and `clang` >= 3.4) 290 | 291 | If your problems nonetheless persist, please file an [issue](https://github.com/const-ae/sparseMatrixStats/issues/) including the following information: 292 | 293 | * Operating system with exact version (e.g. 'Linux Ubuntu 18.04') 294 | * Compiler and compiler version (e.g. 'gcc version 7.2.5') 295 | * The output of `sessionInfo()` 296 | * Information if you have a `~/.R/Makevars` file and what it contains 297 | * The exact call that you use to install `sparseMatrixStats` including the full error message 298 | 299 | -------------------------------------------------------------------------------- /R/methods_row.R: -------------------------------------------------------------------------------- 1 | 2 | 3 | # Sum 4 | 5 | #' @rdname colSums2-xgCMatrix-method 6 | #' @export 7 | setMethod("rowSums2", signature(x = "xgCMatrix"), 8 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 9 | if(! is.null(rows)){ 10 | x <- x[rows, , drop = FALSE] 11 | } 12 | if(is.logical(cols) && length(cols) == ncol(x)){ 13 | set_result_names_t(dgCMatrix_rowSums2_bool_col_select(x, na_rm = na.rm, col_selector = cols), useNames) 14 | }else if(is.integer(cols) || is.numeric(cols)){ 15 | set_result_names_t(dgCMatrix_rowSums2_int_col_select(x, na_rm = na.rm, col_selector = cols), useNames) 16 | }else{ 17 | if(! is.null(cols)){ 18 | x <- x[, cols, drop = FALSE] 19 | } 20 | # dgCMatrix_colSums2(t(x), na_rm = na.rm) 21 | set_result_names_t(dgCMatrix_rowSums2(x, na_rm = na.rm), useNames) 22 | } 23 | }) 24 | 25 | 26 | 27 | # Mean 28 | 29 | #' @rdname colMeans2-xgCMatrix-method 30 | #' @export 31 | setMethod("rowMeans2", signature(x = "xgCMatrix"), 32 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 33 | if(! is.null(rows)){ 34 | x <- x[rows, , drop = FALSE] 35 | } 36 | if(is.logical(cols) && length(cols) == ncol(x)){ 37 | set_result_names_t(dgCMatrix_rowMeans2_bool_col_select(x, na_rm = na.rm, col_selector = cols), useNames) 38 | }else if(is.integer(cols) || is.numeric(cols)){ 39 | set_result_names_t(dgCMatrix_rowMeans2_int_col_select(x, na_rm = na.rm, col_selector = cols), useNames) 40 | }else{ 41 | # dgCMatrix_colMeans2(t(x), na_rm = na.rm) 42 | set_result_names_t(dgCMatrix_rowMeans2(x, na_rm = na.rm), useNames) 43 | } 44 | }) 45 | 46 | 47 | # Median 48 | 49 | #' @rdname colMedians-dgCMatrix-method 50 | #' @export 51 | setMethod("rowMedians", signature(x = "dgCMatrix"), 52 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 53 | if(! is.null(rows)){ 54 | x <- x[rows, , drop = FALSE] 55 | } 56 | if(! is.null(cols)){ 57 | x <- x[, cols, drop = FALSE] 58 | } 59 | set_result_names_t(dgCMatrix_colMedians(t(x), na_rm = na.rm), useNames) 60 | }) 61 | 62 | 63 | # Vars 64 | 65 | #' @rdname colVars-xgCMatrix-method 66 | #' @export 67 | setMethod("rowVars", signature(x = "xgCMatrix"), 68 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, center = NULL, useNames = TRUE){ 69 | if(! is.null(center) && length(center) != nrow(x)){ 70 | stop("Argument 'center' must be the same length as number of rows of 'x': ", length(center), "!=", nrow(x)) 71 | } 72 | if(! is.null(rows)){ 73 | x <- x[rows, , drop = FALSE] 74 | } 75 | if(! is.null(cols)){ 76 | x <- x[, cols, drop = FALSE] 77 | } 78 | # dgCMatrix_colVars(t(x), na_rm = na.rm) 79 | set_result_names_t(dgCMatrix_rowVars(x, na_rm = na.rm, center = center), useNames, default = ncol(x) > 0 && !is.null(center)) 80 | }) 81 | 82 | 83 | # Sds 84 | 85 | #' @rdname colSds-xgCMatrix-method 86 | #' @export 87 | setMethod("rowSds", signature(x = "xgCMatrix"), 88 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, center = NULL, useNames = TRUE){ 89 | if(! is.null(center) && length(center) != nrow(x)){ 90 | stop("Argument 'center' must be the same length as number of rows of 'x': ", length(center), "!=", nrow(x)) 91 | } 92 | if(! is.null(rows)){ 93 | x <- x[rows, , drop = FALSE] 94 | } 95 | if(! is.null(cols)){ 96 | x <- x[, cols, drop = FALSE] 97 | } 98 | set_result_names_t(sqrt(dgCMatrix_rowVars(x, na_rm = na.rm, center = center)), useNames) 99 | }) 100 | 101 | 102 | 103 | # Mads 104 | 105 | #' @rdname colMads-dgCMatrix-method 106 | #' @export 107 | setMethod("rowMads", signature(x = "dgCMatrix"), 108 | function(x, rows = NULL, cols = NULL, center = NULL, constant = 1.4826, na.rm=FALSE, useNames = TRUE){ 109 | if(! is.null(center) && length(center) != nrow(x)){ 110 | stop("Argument 'center' must be the same length as number of rows of 'x': ", length(center), "!=", nrow(x)) 111 | } 112 | colMads(t(x), rows = cols, cols = rows, center = center, constant = constant, na.rm = na.rm, useNames = useNames) 113 | }) 114 | 115 | 116 | # LogSumExp 117 | 118 | #' @rdname colLogSumExps-xgCMatrix-method 119 | #' @export 120 | setMethod("rowLogSumExps", signature(lx = "xgCMatrix"), 121 | function(lx, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 122 | if(! is.null(rows)){ 123 | lx <- lx[rows, , drop = FALSE] 124 | } 125 | if(! is.null(cols)){ 126 | lx <- lx[, cols, drop = FALSE] 127 | } 128 | set_result_names_t(dgCMatrix_colLogSumExps(t(lx), na_rm = na.rm), useNames, default = TRUE, names = rownames(lx)) 129 | }) 130 | 131 | 132 | # Prods 133 | 134 | #' @rdname colProds-xgCMatrix-method 135 | #' @export 136 | setMethod("rowProds", signature(x = "xgCMatrix"), 137 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 138 | if(! is.null(rows)){ 139 | x <- x[rows, , drop = FALSE] 140 | } 141 | if(! is.null(cols)){ 142 | x <- x[, cols, drop = FALSE] 143 | } 144 | set_result_names_t(dgCMatrix_colProds(t(x), na_rm = na.rm), useNames) 145 | }) 146 | 147 | 148 | 149 | # Min 150 | 151 | #' @rdname colMins-dgCMatrix-method 152 | #' @export 153 | setMethod("rowMins", signature(x = "dgCMatrix"), 154 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 155 | if(! is.null(rows)){ 156 | x <- x[rows, , drop = FALSE] 157 | } 158 | if(! is.null(cols)){ 159 | x <- x[, cols, drop = FALSE] 160 | } 161 | set_result_names_t(dgCMatrix_colMins(t(x), na_rm = na.rm), useNames) 162 | }) 163 | 164 | 165 | # Max 166 | 167 | #' @rdname colMaxs-dgCMatrix-method 168 | #' @export 169 | setMethod("rowMaxs", signature(x = "dgCMatrix"), 170 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 171 | if(! is.null(rows)){ 172 | x <- x[rows, , drop = FALSE] 173 | } 174 | if(! is.null(cols)){ 175 | x <- x[, cols, drop = FALSE] 176 | } 177 | set_result_names_t(dgCMatrix_colMaxs(t(x), na_rm = na.rm), useNames) 178 | }) 179 | 180 | 181 | # OrderStats 182 | 183 | #' @rdname colOrderStats-dgCMatrix-method 184 | #' @export 185 | setMethod("rowOrderStats", signature(x = "dgCMatrix"), 186 | function(x, rows = NULL, cols = NULL, which = 1, na.rm=FALSE, useNames = TRUE){ 187 | if(which < 1 || which > ncol(x)){ 188 | stop("Argument 'which' is out of range.") 189 | } 190 | if(! is.null(rows)){ 191 | x <- x[rows, , drop = FALSE] 192 | } 193 | if(! is.null(cols)){ 194 | x <- x[, cols, drop = FALSE] 195 | } 196 | set_result_names_t(dgCMatrix_colOrderStats(t(x), which = which, na_rm = na.rm), useNames) 197 | }) 198 | 199 | 200 | 201 | # Weighted Means 202 | 203 | #' @rdname colWeightedMeans-xgCMatrix-method 204 | #' @export 205 | setMethod("rowWeightedMeans", signature(x = "xgCMatrix"), 206 | function(x, w = NULL, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 207 | colWeightedMeans(t(x), w = w, rows = cols, cols = rows, na.rm = na.rm, useNames = useNames) 208 | }) 209 | 210 | 211 | # Weighted Medians 212 | 213 | #' @rdname colWeightedMedians-dgCMatrix-method 214 | #' @export 215 | setMethod("rowWeightedMedians", signature(x = "dgCMatrix"), 216 | function(x, w = NULL, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 217 | colWeightedMedians(t(x), w = w, rows = cols, cols = rows, na.rm = na.rm, useNames = useNames) 218 | }) 219 | 220 | 221 | # Weighted Vars 222 | 223 | #' @rdname colWeightedVars-xgCMatrix-method 224 | #' @export 225 | setMethod("rowWeightedVars", signature(x = "xgCMatrix"), 226 | function(x, w = NULL, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 227 | colWeightedVars(t(x), w = w, rows = cols, cols = rows, na.rm = na.rm, useNames = useNames) 228 | }) 229 | 230 | 231 | 232 | # Weighted Sds 233 | 234 | #' @rdname colWeightedSds-xgCMatrix-method 235 | #' @export 236 | setMethod("rowWeightedSds", signature(x = "xgCMatrix"), 237 | function(x, w = NULL, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 238 | if(! is.null(rows)){ 239 | x <- x[rows, , drop = FALSE] 240 | } 241 | if(! is.null(cols)){ 242 | x <- x[, cols, drop = FALSE] 243 | w <- w[cols] 244 | } 245 | if(is.null(w)){ 246 | set_result_names_t(sqrt(dgCMatrix_rowVars(x, na_rm = na.rm, center = NULL)), useNames, default = TRUE) 247 | }else{ 248 | set_result_names_t(sqrt(dgCMatrix_colWeightedVars(t(x), weights = w, na_rm = na.rm)), useNames, default = TRUE) 249 | } 250 | }) 251 | 252 | 253 | # Weighted Mads 254 | 255 | #' @rdname colWeightedMads-dgCMatrix-method 256 | #' @export 257 | setMethod("rowWeightedMads", signature(x = "dgCMatrix"), 258 | function(x, w = NULL, rows = NULL, cols = NULL, na.rm=FALSE, constant = 1.4826, center = NULL, useNames = TRUE){ 259 | colWeightedMads(t(x), w=w, rows = cols, cols = rows, na.rm=na.rm, constant = constant, center = center, useNames = useNames) 260 | }) 261 | 262 | 263 | 264 | # Count 265 | 266 | #' @rdname colCounts-xgCMatrix-method 267 | #' @export 268 | setMethod("rowCounts", signature(x = "xgCMatrix"), 269 | function(x, rows = NULL, cols = NULL, value = TRUE, na.rm=FALSE, useNames = TRUE){ 270 | stopifnot(length(value) == 1) 271 | if(is(x, "lgCMatrix")){ 272 | value <- as.logical(value) 273 | } 274 | if(! is.null(rows)){ 275 | x <- x[rows, , drop = FALSE] 276 | } 277 | if(! is.null(cols)){ 278 | x <- x[, cols, drop = FALSE] 279 | } 280 | set_result_names_t(dgCMatrix_colCounts(t(x), value, na_rm = na.rm), useNames) 281 | }) 282 | 283 | 284 | # AnyNA 285 | 286 | #' @rdname colAnyNAs-xgCMatrix-method 287 | #' @export 288 | setMethod("rowAnyNAs", signature(x = "xgCMatrix"), 289 | function(x, rows = NULL, cols = NULL, useNames = TRUE){ 290 | if(! is.null(rows)){ 291 | x <- x[rows, , drop = FALSE] 292 | } 293 | if(! is.null(cols)){ 294 | x <- x[, cols, drop = FALSE] 295 | } 296 | set_result_names_t(dgCMatrix_colAnyNAs(t(x)), useNames) 297 | }) 298 | 299 | 300 | # Anys 301 | 302 | #' @rdname colAnys-xgCMatrix-method 303 | #' @export 304 | setMethod("rowAnys", signature(x = "xgCMatrix"), 305 | function(x, rows = NULL, cols = NULL, value = TRUE, na.rm=FALSE, useNames = TRUE){ 306 | stopifnot(length(value) == 1) 307 | if(is(x, "lgCMatrix")){ 308 | value <- as.logical(value) 309 | } 310 | if(! is.null(rows)){ 311 | x <- x[rows, , drop = FALSE] 312 | } 313 | if(! is.null(cols)){ 314 | x <- x[, cols, drop = FALSE] 315 | } 316 | res <- if(isTRUE(value)){ 317 | ! dgCMatrix_colAlls(t(x), value = 0, na_rm=na.rm) 318 | }else{ 319 | dgCMatrix_colAnys(t(x), value, na_rm=na.rm) 320 | } 321 | set_result_names_t(res, useNames) 322 | }) 323 | 324 | 325 | 326 | # Alls 327 | 328 | #' @rdname colAlls-xgCMatrix-method 329 | #' @export 330 | setMethod("rowAlls", signature(x = "xgCMatrix"), 331 | function(x, rows = NULL, cols = NULL, value = TRUE, na.rm=FALSE, useNames = TRUE){ 332 | stopifnot(length(value) == 1) 333 | if(is(x, "lgCMatrix")){ 334 | value <- as.logical(value) 335 | } 336 | if(! is.null(rows)){ 337 | x <- x[rows, , drop = FALSE] 338 | } 339 | if(! is.null(cols)){ 340 | x <- x[, cols, drop = FALSE] 341 | } 342 | res <- if(isTRUE(value)){ 343 | ! dgCMatrix_colAnys(t(x), value = 0, na_rm = na.rm) 344 | }else{ 345 | dgCMatrix_colAlls(t(x), value, na_rm=na.rm) 346 | } 347 | set_result_names_t(res, useNames) 348 | }) 349 | 350 | 351 | 352 | # Collapse 353 | 354 | #' @rdname colCollapse-xgCMatrix-method 355 | #' @export 356 | setMethod("rowCollapse", signature(x = "xgCMatrix"), 357 | function(x, idxs, rows = NULL, useNames = TRUE){ 358 | idxs <- rep(idxs, length.out = nrow(x)) 359 | if (!is.null(rows)) { 360 | x <- x[rows, , drop = FALSE] 361 | idxs <- idxs[rows] 362 | } 363 | cols <- seq_len(ncol(x)) - 1L 364 | cols <- cols[idxs] 365 | idxs <- nrow(x) * cols + seq_len(nrow(x)) 366 | cols <- NULL 367 | set_result_names_t(x[idxs], useNames) 368 | }) 369 | 370 | 371 | 372 | 373 | # Quantiles 374 | 375 | #' @rdname colQuantiles-xgCMatrix-method 376 | #' @export 377 | setMethod("rowQuantiles", signature(x = "xgCMatrix"), 378 | function(x, rows = NULL, cols = NULL, probs = seq(from = 0, to = 1, by = 0.25), na.rm=FALSE, type = 7L, useNames = TRUE, drop = TRUE){ 379 | colQuantiles(t(x), rows = cols, cols = rows, probs = probs, na.rm = na.rm, type = type, useNames = useNames, drop = drop) 380 | }) 381 | 382 | 383 | 384 | # Tabulates 385 | 386 | #' @rdname colTabulates-xgCMatrix-method 387 | #' @export 388 | setMethod("rowTabulates", signature(x = "xgCMatrix"), 389 | function(x, rows = NULL, cols = NULL, values = NULL, useNames = TRUE){ 390 | colTabulates(t(x), rows = cols, cols = rows, values = values, useNames = useNames) 391 | }) 392 | 393 | 394 | 395 | # IQRs 396 | 397 | #' @rdname colIQRs-xgCMatrix-method 398 | #' @export 399 | setMethod("rowIQRs", signature(x = "xgCMatrix"), 400 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 401 | if(! is.null(rows)){ 402 | x <- x[rows, , drop = FALSE] 403 | } 404 | if(! is.null(cols)){ 405 | x <- x[, cols, drop = FALSE] 406 | } 407 | col_q <- colQuantiles(t(x), probs=c(0.25, 0.75), na.rm = na.rm, useNames = TRUE, drop = FALSE) 408 | if(is.na(useNames)){ 409 | useNames <- FALSE 410 | } 411 | set_result_names_t(unname(col_q[,2] - col_q[,1]), useNames, names = rownames(col_q)) 412 | }) 413 | 414 | 415 | 416 | # Ranges 417 | 418 | #' @rdname colRanges-dgCMatrix-method 419 | #' @export 420 | setMethod("rowRanges", signature(x = "dgCMatrix"), 421 | function(x, rows = NULL, cols = NULL, na.rm=FALSE, useNames = TRUE){ 422 | tx <- t(x) 423 | row_max <- colMaxs(tx, rows = cols, cols = rows, na.rm = na.rm, useNames = TRUE) 424 | row_min <- colMins(tx, rows = cols, cols = rows, na.rm = na.rm, useNames = TRUE) 425 | if(is.na(useNames)){ 426 | useNames <- FALSE 427 | } 428 | set_result_rownames(unname(cbind(row_min, row_max)), useNames, names = names(row_max)) 429 | }) 430 | 431 | 432 | 433 | # Cumsums 434 | 435 | #' @rdname colCumsums-xgCMatrix-method 436 | #' @export 437 | setMethod("rowCumsums", signature(x = "xgCMatrix"), 438 | function(x, rows = NULL, cols = NULL, useNames = TRUE){ 439 | if(! is.null(rows)){ 440 | x <- x[rows, , drop = FALSE] 441 | } 442 | if(! is.null(cols)){ 443 | x <- x[, cols, drop = FALSE] 444 | } 445 | mat <- t(dgCMatrix_colCumsums(t(x))) 446 | mat <- set_result_rownames(mat, useNames, names = rownames(x)) 447 | mat <- set_result_colnames(mat, useNames, names = colnames(x)) 448 | mat 449 | }) 450 | 451 | 452 | 453 | # Cumprods 454 | 455 | #' @rdname colCumprods-xgCMatrix-method 456 | #' @export 457 | setMethod("rowCumprods", signature(x = "xgCMatrix"), 458 | function(x, rows = NULL, cols = NULL, useNames = TRUE){ 459 | if(! is.null(rows)){ 460 | x <- x[rows, , drop = FALSE] 461 | } 462 | if(! is.null(cols)){ 463 | x <- x[, cols, drop = FALSE] 464 | } 465 | mat <- t(dgCMatrix_colCumprods(t(x))) 466 | mat <- set_result_rownames(mat, useNames, names = rownames(x)) 467 | mat <- set_result_colnames(mat, useNames, names = colnames(x)) 468 | mat 469 | }) 470 | 471 | 472 | 473 | # Cummins 474 | 475 | #' @rdname colCummins-dgCMatrix-method 476 | #' @export 477 | setMethod("rowCummins", signature(x = "dgCMatrix"), 478 | function(x, rows = NULL, cols = NULL, useNames = TRUE){ 479 | if(! is.null(rows)){ 480 | x <- x[rows, , drop = FALSE] 481 | } 482 | if(! is.null(cols)){ 483 | x <- x[, cols, drop = FALSE] 484 | } 485 | mat <- t(dgCMatrix_colCummins(t(x))) 486 | mat <- set_result_rownames(mat, useNames, names = rownames(x)) 487 | mat <- set_result_colnames(mat, useNames, names = colnames(x)) 488 | mat 489 | }) 490 | 491 | 492 | 493 | # Cummaxs 494 | 495 | #' @rdname colCummaxs-dgCMatrix-method 496 | #' @export 497 | setMethod("rowCummaxs", signature(x = "dgCMatrix"), 498 | function(x, rows = NULL, cols = NULL, useNames = TRUE){ 499 | if(! is.null(rows)){ 500 | x <- x[rows, , drop = FALSE] 501 | } 502 | if(! is.null(cols)){ 503 | x <- x[, cols, drop = FALSE] 504 | } 505 | mat <- t(dgCMatrix_colCummaxs(t(x))) 506 | mat <- set_result_rownames(mat, useNames, names = rownames(x)) 507 | mat <- set_result_colnames(mat, useNames, names = colnames(x)) 508 | mat 509 | }) 510 | 511 | 512 | 513 | # Ranks 514 | 515 | #' @rdname colRanks-dgCMatrix-method 516 | #' @export 517 | setMethod("rowRanks", signature(x = "dgCMatrix"), 518 | function(x, rows = NULL, cols = NULL, ties.method = c("max", "average", "min"), preserveShape = TRUE, na.handling = c("keep", "last"), ..., useNames = TRUE){ 519 | if(! is.null(rows)){ 520 | x <- x[rows, , drop = FALSE] 521 | } 522 | if(! is.null(cols)){ 523 | x <- x[, cols, drop = FALSE] 524 | } 525 | colRanks(t(x), ties.method = ties.method, preserveShape = ! preserveShape, na.handling = na.handling, useNames = useNames) 526 | }) 527 | 528 | 529 | 530 | #' @rdname colDiffs-dgCMatrix-method 531 | 532 | #' @export 533 | setMethod("rowDiffs", signature(x = "dgCMatrix"), 534 | function(x, rows = NULL, cols = NULL, lag = 1L, differences = 1L, useNames = TRUE){ 535 | t(colDiffs(t(x), rows = cols, cols = rows, lag = lag, differences = differences, useNames = useNames)) 536 | }) 537 | 538 | 539 | # VarDiffs 540 | 541 | #' @rdname colVarDiffs-dgCMatrix-method 542 | #' @export 543 | setMethod("rowVarDiffs", signature(x = "dgCMatrix"), 544 | function(x, rows = NULL, cols = NULL, na.rm = FALSE, diff = 1L, trim = 0, useNames = TRUE){ 545 | colVarDiffs(t(x), rows = cols, cols = rows, na.rm=na.rm, diff=diff, trim = trim, useNames = useNames) 546 | }) 547 | 548 | 549 | 550 | # SdDiffs 551 | 552 | #' @rdname colSdDiffs-dgCMatrix-method 553 | #' @export 554 | setMethod("rowSdDiffs", signature(x = "dgCMatrix"), 555 | function(x, rows = NULL, cols = NULL, na.rm = FALSE, diff = 1L, trim = 0, useNames = TRUE){ 556 | colSdDiffs(t(x), rows = cols, cols = rows, na.rm=na.rm, diff=diff, trim = trim, useNames = useNames) 557 | }) 558 | 559 | 560 | 561 | # MadDiffs 562 | 563 | #' @rdname colMadDiffs-dgCMatrix-method 564 | #' @export 565 | setMethod("rowMadDiffs", signature(x = "dgCMatrix"), 566 | function(x, rows = NULL, cols = NULL, na.rm = FALSE, diff = 1L, trim = 0, constant = 1.4826, ..., useNames = TRUE){ 567 | colMadDiffs(t(x), rows = cols, cols = rows, na.rm=na.rm, diff=diff, trim = trim, constant = constant, useNames = useNames) 568 | }) 569 | 570 | 571 | 572 | # IQRDiffs 573 | 574 | #' @rdname colIQRDiffs-dgCMatrix-method 575 | #' @export 576 | setMethod("rowIQRDiffs", signature(x = "dgCMatrix"), 577 | function(x, rows = NULL, cols = NULL, na.rm = FALSE, diff = 1L, trim = 0, useNames = TRUE){ 578 | colIQRDiffs(t(x), rows = cols, cols = rows, na.rm=na.rm, diff=diff, trim = trim, useNames = useNames) 579 | }) 580 | 581 | 582 | #' @rdname colAvgsPerRowSet-xgCMatrix-method 583 | #' @export 584 | setMethod("rowAvgsPerColSet", signature(X = "xgCMatrix"), 585 | function(X, W = NULL, rows = NULL, S, FUN = rowMeans2, ..., na.rm = NA, tFUN = FALSE){ 586 | tZ <- colAvgsPerRowSet(t(X), W = W, cols = rows, S = S, FUN = FUN, ..., na.rm = na.rm, tFUN = ! tFUN) 587 | t(tZ) 588 | }) 589 | 590 | -------------------------------------------------------------------------------- /tests/testthat/test-row_functions.R: -------------------------------------------------------------------------------- 1 | set.seed(1) 2 | # source("tests/testthat/setup.R") 3 | 4 | mat <- t(make_matrix_with_all_features(nrow=15, ncol=10)) 5 | rownames(mat) <- letters[seq_len(10)] 6 | colnames(mat) <- paste0("col_", seq_len(ncol(mat))) 7 | sp_mat <- as(mat, "dgCMatrix") 8 | row_subset <- 1:5 9 | col_subset <- c(7, 9, 2) 10 | 11 | test_that("rowSums works", { 12 | expect_equal(rowSums2(sp_mat), MatrixGenerics::rowSums2(mat)) 13 | expect_equal(rowSums2(sp_mat, na.rm=TRUE), MatrixGenerics::rowSums2(mat, na.rm=TRUE)) 14 | expect_equal(rowSums2(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowSums2(mat, rows = row_subset, cols = col_subset)) 15 | expect_equal(rowSums2(sp_mat, rows = row_subset, cols = col_subset, na.rm=TRUE), 16 | MatrixGenerics::rowSums2(mat, rows = row_subset, cols = col_subset, na.rm=TRUE)) 17 | 18 | col_selector <- sample(c(TRUE, FALSE), size = ncol(mat), replace = TRUE) 19 | expect_equal(rowSums2(sp_mat, rows = row_subset, cols = col_selector), MatrixGenerics::rowSums2(mat, rows = row_subset, cols = col_selector)) 20 | expect_equal(rowSums2(sp_mat, rows = row_subset, cols = col_selector, na.rm=TRUE), 21 | MatrixGenerics::rowSums2(mat, rows = row_subset, cols = col_selector, na.rm=TRUE)) 22 | 23 | col_selector[2] <- NA 24 | expect_equal(rowSums2(sp_mat, rows = row_subset, cols = col_selector), 25 | MatrixGenerics::rowSums2(mat, rows = row_subset, cols = col_selector)) 26 | expect_equal(rowSums2(sp_mat, cols = col_selector), 27 | rowSums(mat[,col_selector,drop=FALSE])) 28 | 29 | col_selector <- c(1, 3, 5, NA, 1) 30 | expect_equal(rowSums2(sp_mat, rows = row_subset, cols = col_selector), 31 | MatrixGenerics::rowSums2(mat, rows = row_subset, cols = col_selector)) 32 | expect_equal(rowSums2(sp_mat, cols = col_selector), 33 | rowSums(mat[,col_selector,drop=FALSE])) 34 | }) 35 | 36 | 37 | test_that("rowMeans works", { 38 | expect_equal(rowMeans2(sp_mat), MatrixGenerics::rowMeans2(mat)) 39 | expect_equal(rowMeans2(sp_mat, na.rm=TRUE), MatrixGenerics::rowMeans2(mat, na.rm=TRUE)) 40 | expect_equal(rowMeans2(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowMeans2(mat, rows = row_subset, cols = col_subset)) 41 | expect_equal(rowMeans2(sp_mat, rows = row_subset, cols = col_subset, na.rm = TRUE), 42 | MatrixGenerics::rowMeans2(mat, rows = row_subset, cols = col_subset, na.rm = TRUE)) 43 | 44 | col_selector <- sample(c(TRUE, FALSE), size = ncol(mat), replace = TRUE) 45 | expect_equal(rowMeans2(sp_mat, rows = row_subset, cols = col_selector), MatrixGenerics::rowMeans2(mat, rows = row_subset, cols = col_selector)) 46 | expect_equal(rowMeans2(sp_mat, rows = row_subset, cols = col_selector, na.rm=TRUE), 47 | MatrixGenerics::rowMeans2(mat, rows = row_subset, cols = col_selector, na.rm=TRUE)) 48 | 49 | col_selector[2] <- NA 50 | expect_equal(rowSums2(sp_mat, rows = row_subset, cols = col_selector), 51 | MatrixGenerics::rowSums2(mat, rows = row_subset, cols = col_selector)) 52 | expect_equal(rowSums2(sp_mat, cols = col_selector), 53 | rowSums(mat[,col_selector,drop=FALSE])) 54 | 55 | 56 | col_selector <- c(1, 3, 5, NA, 1) 57 | expect_equal(rowMeans2(sp_mat, rows = row_subset, cols = col_selector), 58 | MatrixGenerics::rowMeans2(mat, rows = row_subset, cols = col_selector)) 59 | expect_equal(rowMeans2(sp_mat, rows = row_subset, cols = col_selector, na.rm=TRUE), 60 | MatrixGenerics::rowMeans2(mat, rows = row_subset, cols = col_selector, na.rm=TRUE)) 61 | expect_equal(rowSums2(sp_mat, cols = col_selector), 62 | rowSums(mat[,col_selector,drop=FALSE])) 63 | }) 64 | 65 | 66 | test_that("rowMedians works", { 67 | expect_equal(rowMedians(sp_mat), MatrixGenerics::rowMedians(mat)) 68 | expect_equal(rowMedians(sp_mat, na.rm=TRUE), MatrixGenerics::rowMedians(mat, na.rm=TRUE)) 69 | expect_equal(rowMedians(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowMedians(mat, rows = row_subset, cols = col_subset)) 70 | }) 71 | 72 | 73 | 74 | test_that("rowVars works", { 75 | expect_equal(rowVars(sp_mat), MatrixGenerics::rowVars(mat)) 76 | expect_equal(rowVars(sp_mat, na.rm=TRUE), MatrixGenerics::rowVars(mat, na.rm=TRUE)) 77 | expect_equal(rowVars(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowVars(mat, rows = row_subset, cols = col_subset)) 78 | 79 | center <- rowMeans2(sp_mat) 80 | expect_equal(rowVars(sp_mat, center = center), MatrixGenerics::rowVars(mat, center = center)) 81 | }) 82 | 83 | test_that("rowSds works", { 84 | expect_equal(rowSds(sp_mat), MatrixGenerics::rowSds(mat)) 85 | expect_equal(rowSds(sp_mat, na.rm=TRUE), MatrixGenerics::rowSds(mat, na.rm=TRUE)) 86 | expect_equal(rowSds(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowSds(mat, rows = row_subset, cols = col_subset)) 87 | }) 88 | 89 | 90 | test_that("rowMads works", { 91 | expect_equal(rowMads(sp_mat), MatrixGenerics::rowMads(mat)) 92 | expect_equal(rowMads(sp_mat, na.rm=TRUE), MatrixGenerics::rowMads(mat, na.rm=TRUE)) 93 | expect_equal(rowMads(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowMads(mat, rows = row_subset, cols = col_subset)) 94 | 95 | center <- rowMeans2(sp_mat) 96 | expect_equal(rowMads(sp_mat, center = center), MatrixGenerics::rowMads(mat, center = center)) 97 | }) 98 | 99 | test_that("rowMins works", { 100 | expect_equal(rowMins(sp_mat), MatrixGenerics::rowMins(mat)) 101 | expect_equal(rowMins(sp_mat, na.rm=TRUE), MatrixGenerics::rowMins(mat, na.rm=TRUE)) 102 | expect_equal(rowMins(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowMins(mat, rows = row_subset, cols = col_subset)) 103 | }) 104 | 105 | 106 | test_that("rowMaxs works", { 107 | expect_equal(rowMaxs(sp_mat), MatrixGenerics::rowMaxs(mat)) 108 | expect_equal(rowMaxs(sp_mat, na.rm=TRUE), MatrixGenerics::rowMaxs(mat, na.rm=TRUE)) 109 | expect_equal(rowMaxs(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowMaxs(mat, rows = row_subset, cols = col_subset)) 110 | }) 111 | 112 | 113 | 114 | test_that("rowCounts works", { 115 | expect_equal(rowCounts(sp_mat, value=0), MatrixGenerics::rowCounts(mat, value=0)) 116 | expect_equal(rowCounts(sp_mat, na.rm=TRUE, value=0), MatrixGenerics::rowCounts(mat, na.rm=TRUE, value = 0)) 117 | expect_equal(rowCounts(sp_mat, value = tail(t(sp_mat)@x, n=1)), MatrixGenerics::rowCounts(mat, value = tail(t(sp_mat)@x, n=1))) 118 | expect_equal(rowCounts(sp_mat, na.rm=TRUE, value = tail(t(sp_mat)@x, n=1)), MatrixGenerics::rowCounts(mat, na.rm=TRUE, value = tail(t(sp_mat)@x, n=1))) 119 | expect_equal(rowCounts(sp_mat, value=0, rows = row_subset, cols = col_subset), MatrixGenerics::rowCounts(mat, value=0, rows = row_subset, cols = col_subset)) 120 | }) 121 | 122 | 123 | test_that("rowAnyNAs works", { 124 | empty_mat <- matrix(numeric(0), ncol=0, nrow=5) 125 | sp_empty_mat <- as(empty_mat, "dgCMatrix") 126 | expect_equal(rowAnyNAs(sp_mat), MatrixGenerics::rowAnyNAs(mat)) 127 | expect_equal(rowAnyNAs(sp_empty_mat), MatrixGenerics::rowAnyNAs(empty_mat)) 128 | expect_equal(rowAnyNAs(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowAnyNAs(mat, rows = row_subset, cols = col_subset)) 129 | }) 130 | 131 | 132 | test_that("rowAnys works", { 133 | empty_mat <- matrix(numeric(0), ncol=0, nrow=5) 134 | sp_empty_mat <- as(empty_mat, "dgCMatrix") 135 | expect_equal(rowAnys(sp_mat, value=0), MatrixGenerics::rowAnys(mat, value=0)) 136 | expect_equal(rowAnys(sp_mat, na.rm=TRUE, value=0), MatrixGenerics::rowAnys(mat, na.rm=TRUE, value = 0)) 137 | # expect_equal(rowAnys(sp_mat, value=NA), MatrixGenerics::rowAnys(mat, value=NA)) 138 | # expect_equal(rowAnys(sp_mat, na.rm=TRUE, value=NA), MatrixGenerics::rowAnys(mat, na.rm=TRUE, value = NA)) 139 | expect_equal(rowAnys(sp_mat, value = tail(t(sp_mat)@x, n=1)), MatrixGenerics::rowAnys(mat, value = tail(t(sp_mat)@x, n=1))) 140 | expect_equal(rowAnys(sp_mat, na.rm=TRUE, value = tail(t(sp_mat)@x, n=1)), MatrixGenerics::rowAnys(mat, na.rm=TRUE, value = tail(t(sp_mat)@x, n=1))) 141 | expect_equal(rowAnys(sp_mat, value=0, rows = row_subset, cols = col_subset), MatrixGenerics::rowAnys(mat, value=0, rows = row_subset, cols = col_subset)) 142 | }) 143 | 144 | 145 | test_that("rowAlls works", { 146 | empty_mat <- matrix(numeric(0), nrow=5, ncol=0) 147 | sp_empty_mat <- as(empty_mat, "dgCMatrix") 148 | expect_equal(rowAlls(sp_mat, value=0), MatrixGenerics::rowAlls(mat, value=0)) 149 | expect_equal(rowAlls(sp_mat, na.rm=TRUE, value=0), MatrixGenerics::rowAlls(mat, na.rm=TRUE, value = 0)) 150 | # expect_equal(rowAnys(sp_mat, value=NA), MatrixGenerics::rowAnys(mat, value=NA)) 151 | # expect_equal(rowAnys(sp_mat, na.rm=TRUE, value=NA), MatrixGenerics::rowAnys(mat, na.rm=TRUE, value = NA)) 152 | expect_equal(rowAlls(sp_mat, value = tail(t(sp_mat)@x, n=1)), MatrixGenerics::rowAlls(mat, value = tail(t(sp_mat)@x, n=1))) 153 | expect_equal(rowAlls(sp_mat, na.rm=TRUE, value = tail(t(sp_mat)@x, n=1)), MatrixGenerics::rowAlls(mat, na.rm=TRUE, value = tail(t(sp_mat)@x, n=1))) 154 | expect_equal(rowAlls(sp_mat, value=0, rows = row_subset, cols = col_subset), MatrixGenerics::rowAlls(mat, value=0, rows = row_subset, cols = col_subset)) 155 | }) 156 | 157 | 158 | test_that("rowLogSumExps works", { 159 | expect_equal(rowLogSumExps(sp_mat), MatrixGenerics::rowLogSumExps(mat)) 160 | expect_equal(rowLogSumExps(sp_mat, na.rm=TRUE), MatrixGenerics::rowLogSumExps(mat, na.rm=TRUE)) 161 | expect_equal(rowLogSumExps(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowLogSumExps(mat, rows = row_subset, cols = col_subset)) 162 | }) 163 | 164 | 165 | test_that("rowProds works", { 166 | expect_equal(rowProds(sp_mat), MatrixGenerics::rowProds(mat)) 167 | expect_equal(rowProds(sp_mat, na.rm=TRUE), MatrixGenerics::rowProds(mat, na.rm=TRUE)) 168 | expect_equal(rowProds(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowProds(mat, rows = row_subset, cols = col_subset)) 169 | }) 170 | 171 | test_that("rowQuantiles works", { 172 | expect_equal(rowQuantiles(sp_mat), MatrixGenerics::rowQuantiles(mat)) 173 | expect_equal(rowQuantiles(sp_mat, na.rm=TRUE), MatrixGenerics::rowQuantiles(mat, na.rm=TRUE)) 174 | expect_equal(rowQuantiles(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowQuantiles(mat, rows = row_subset, cols = col_subset)) 175 | 176 | # different quantile algorithms work 177 | y <- rpois(n = 21, lambda = 1.5) 178 | for(t in 1:9){ 179 | expect_equal(rowQuantiles(matrix(y, nrow = 1), type = t, probs = seq(0, 1, length.out = 13), drop = TRUE), 180 | rowQuantiles(as(matrix(y, nrow = 1), "dgCMatrix"), type = t, probs = seq(0, 1, length.out = 13), drop = TRUE)) 181 | } 182 | 183 | }) 184 | 185 | 186 | test_that("rowTabulates works", { 187 | suppressWarnings({ # Suppress warning of Inf -> NA 188 | int_mat <- matrix(as.integer(mat), nrow = nrow(mat), ncol = ncol(mat)) 189 | }) 190 | int_sp_mat <- as(int_mat, "dgCMatrix") 191 | expect_equal(rowTabulates(int_sp_mat), MatrixGenerics::rowTabulates(int_mat)) 192 | values <- c(0, -2, NA, 3, 17) 193 | expect_equal(rowTabulates(int_sp_mat, values = values), MatrixGenerics::rowTabulates(int_mat, values = values)) 194 | expect_equal(rowTabulates(int_sp_mat, values = values, rows = row_subset, cols = col_subset), MatrixGenerics::rowTabulates(int_mat, values = values, rows = row_subset, cols = col_subset)) 195 | }) 196 | 197 | 198 | test_that("rowOrderStats works", { 199 | no_na_mat <- mat 200 | no_na_mat[is.na(no_na_mat)] <- 99 201 | no_na_sp_mat <- as(no_na_mat, "dgCMatrix") 202 | 203 | expect_equal(rowOrderStats(no_na_sp_mat, which = 1), MatrixGenerics::rowOrderStats(no_na_mat, which = 1)) 204 | expect_equal(rowOrderStats(no_na_sp_mat, which = 6), MatrixGenerics::rowOrderStats(no_na_mat, which = 6)) 205 | expect_error(rowOrderStats(no_na_sp_mat, which = 110)) # which should be larger than nrow(no_na_mat) 206 | expect_error(MatrixGenerics::rowOrderStats(no_na_mat, which = 110)) 207 | expect_equal(rowOrderStats(no_na_sp_mat, which = 1, rows = row_subset, cols = col_subset), MatrixGenerics::rowOrderStats(no_na_mat, which = 1, rows = row_subset, cols = col_subset)) 208 | skip("MatrixGenerics::xxxOrderStats() does not support missing values") 209 | expect_equal(rowOrderStats(sp_mat, which = 6), MatrixGenerics::rowOrderStats(mat, which = 6)) 210 | expect_equal(rowOrderStats(sp_mat, which = 10, na.rm=TRUE), MatrixGenerics::rowOrderStats(mat, which = 6, na.rm=TRUE)) 211 | }) 212 | 213 | 214 | 215 | test_that("cumulative functions work", { 216 | expect_equal(rowCumsums(sp_mat), MatrixGenerics::rowCumsums(mat)) 217 | expect_equal(rowCumprods(sp_mat), MatrixGenerics::rowCumprods(mat)) 218 | expect_equal(rowCummins(sp_mat), MatrixGenerics::rowCummins(mat)) 219 | expect_equal(rowCummaxs(sp_mat), MatrixGenerics::rowCummaxs(mat)) 220 | 221 | expect_equal(rowCumsums(sp_mat, useNames = TRUE), MatrixGenerics::rowCumsums(mat, useNames = TRUE)) 222 | expect_equal(rowCumprods(sp_mat, useNames = TRUE), MatrixGenerics::rowCumprods(mat, useNames = TRUE)) 223 | expect_equal(rowCummins(sp_mat, useNames = TRUE), MatrixGenerics::rowCummins(mat, useNames = TRUE)) 224 | expect_equal(rowCummaxs(sp_mat, useNames = TRUE), MatrixGenerics::rowCummaxs(mat, useNames = TRUE)) 225 | 226 | 227 | expect_equal(rowCumsums(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowCumsums(mat, rows = row_subset, cols = col_subset)) 228 | expect_equal(rowCumprods(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowCumprods(mat, rows = row_subset, cols = col_subset)) 229 | expect_equal(rowCummins(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowCummins(mat, rows = row_subset, cols = col_subset)) 230 | expect_equal(rowCummaxs(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowCummaxs(mat, rows = row_subset, cols = col_subset)) 231 | 232 | 233 | # There is no na.rm version 234 | }) 235 | 236 | test_that("rowIQRs works", { 237 | expect_equal(rowIQRs(sp_mat), MatrixGenerics::rowIQRs(mat)) 238 | expect_equal(rowIQRs(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowIQRs(mat, rows = row_subset, cols = col_subset)) 239 | }) 240 | 241 | test_that("rowRanges works", { 242 | expect_equal(rowRanges(sp_mat), MatrixGenerics::rowRanges(mat)) 243 | expect_equal(rowRanges(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowRanges(mat, rows = row_subset, cols = col_subset)) 244 | }) 245 | 246 | 247 | test_that("rowRanks works", { 248 | expect_equal(rowRanks(sp_mat), MatrixGenerics::rowRanks(mat)) 249 | expect_equal(rowRanks(sp_mat, ties.method = "average"), MatrixGenerics::rowRanks(mat, ties.method = "average")) 250 | 251 | expect_equal(rowRanks(sp_mat, rows = row_subset, cols = col_subset), MatrixGenerics::rowRanks(mat, rows = row_subset, cols = col_subset)) 252 | }) 253 | 254 | 255 | 256 | test_that("rowWeightedMeans works", { 257 | weights <- rnorm(ncol(sp_mat), mean=4, sd=0.1) 258 | expect_equal(rowWeightedMeans(sp_mat, w=NULL), MatrixGenerics::rowWeightedMeans(mat, w=NULL)) 259 | expect_equal(rowWeightedMeans(sp_mat, w=weights), MatrixGenerics::rowWeightedMeans(mat, w=weights)) 260 | expect_equal(rowWeightedMeans(sp_mat, na.rm=TRUE, w=weights), MatrixGenerics::rowWeightedMeans(mat, na.rm=TRUE, w=weights)) 261 | expect_equal(rowWeightedMeans(sp_mat, w=weights, rows = row_subset, cols = col_subset), MatrixGenerics::rowWeightedMeans(mat, w=weights, rows = row_subset, cols = col_subset)) 262 | }) 263 | 264 | 265 | test_that("rowWeightedMedians works", { 266 | weights <- rnorm(ncol(sp_mat), mean=4, sd=0.1) 267 | expect_equal(rowWeightedMedians(sp_mat, w=weights), MatrixGenerics::rowWeightedMedians(mat, w=weights, interpolate = FALSE)) 268 | expect_equal(rowWeightedMedians(sp_mat, na.rm=TRUE, w=weights), MatrixGenerics::rowWeightedMedians(mat, w=weights, na.rm=TRUE, interpolate = FALSE)) 269 | expect_equal(rowWeightedMedians(sp_mat, w=weights, rows = row_subset, cols = col_subset), MatrixGenerics::rowWeightedMedians(mat, w=weights, interpolate = FALSE, rows = row_subset, cols = col_subset)) 270 | }) 271 | 272 | 273 | test_that("rowWeightedMads works", { 274 | skip("different result than matrixStats version, because sparseMatrixStats uses `interpolate=FALSE`.") 275 | weights <- rnorm(ncol(sp_mat), mean=4, sd=0.1) 276 | expect_equal(rowWeightedMads(sp_mat, w=weights), MatrixGenerics::rowWeightedMads(mat, w=weights)) 277 | expect_equal(rowWeightedMads(sp_mat, na.rm=TRUE, w=weights), MatrixGenerics::rowWeightedMads(mat, w=weights, na.rm=TRUE)) 278 | expect_equal(rowWeightedMads(sp_mat, w=weights, rows = row_subset, cols = col_subset), MatrixGenerics::rowWeightedMads(mat, w=weights, rows = row_subset, cols = col_subset)) 279 | }) 280 | 281 | 282 | test_that("rowWeightedVars works", { 283 | weights <- rnorm(ncol(sp_mat), mean=4, sd=0.1) 284 | expect_equal(rowWeightedVars(sp_mat, w=weights), MatrixGenerics::rowWeightedVars(mat, w=weights)) 285 | expect_equal(rowWeightedVars(sp_mat, na.rm=TRUE), MatrixGenerics::rowWeightedVars(mat, na.rm=TRUE)) 286 | expect_equal(rowWeightedVars(sp_mat, w=weights, rows = row_subset, cols = col_subset), MatrixGenerics::rowWeightedVars(mat, w=weights, rows = row_subset, cols = col_subset)) 287 | }) 288 | 289 | 290 | test_that("rowWeightedSds works", { 291 | weights <- rnorm(ncol(sp_mat), mean=4, sd=0.1) 292 | expect_equal(rowWeightedSds(sp_mat, w=weights), MatrixGenerics::rowWeightedSds(mat, w=weights)) 293 | expect_equal(rowWeightedSds(sp_mat, na.rm=TRUE), MatrixGenerics::rowWeightedSds(mat, na.rm=TRUE)) 294 | expect_equal(rowWeightedSds(sp_mat, w=weights, rows = row_subset, cols = col_subset), MatrixGenerics::rowWeightedSds(mat, w=weights, rows = row_subset, cols = col_subset)) 295 | }) 296 | 297 | 298 | 299 | test_that("rowXXDiffs work", { 300 | 301 | expect_equal(rowDiffs(sp_mat, diff = 1), MatrixGenerics::rowDiffs(mat, diff = 1)) 302 | expect_equal(rowDiffs(sp_mat, diff = 3), MatrixGenerics::rowDiffs(mat, diff = 3)) 303 | expect_equal(rowDiffs(sp_mat, diff = 3, lag= 2), MatrixGenerics::rowDiffs(mat, diff = 3, lag = 2)) 304 | expect_equal(rowDiffs(sp_mat, diff = 1, rows = row_subset, cols = col_subset), MatrixGenerics::rowDiffs(mat, diff = 1, rows = row_subset, cols = col_subset)) 305 | 306 | expect_equal(rowVarDiffs(sp_mat, diff = 0), MatrixGenerics::rowVarDiffs(mat, diff = 0)) 307 | expect_equal(rowVarDiffs(sp_mat, diff = 1), MatrixGenerics::rowVarDiffs(mat, diff = 1)) 308 | expect_equal(rowVarDiffs(sp_mat, diff = 3), MatrixGenerics::rowVarDiffs(mat, diff = 3)) 309 | expect_equal(rowVarDiffs(sp_mat, na.rm=TRUE), MatrixGenerics::rowVarDiffs(mat, na.rm=TRUE)) 310 | expect_equal(rowVarDiffs(sp_mat, diff = 0, rows = row_subset, cols = col_subset), MatrixGenerics::rowVarDiffs(mat, diff = 0, rows = row_subset, cols = col_subset)) 311 | 312 | expect_equal(rowSdDiffs(sp_mat, diff = 0), MatrixGenerics::rowSdDiffs(mat, diff = 0)) 313 | expect_equal(rowSdDiffs(sp_mat, diff = 1), MatrixGenerics::rowSdDiffs(mat, diff = 1)) 314 | expect_equal(rowSdDiffs(sp_mat, diff = 3), MatrixGenerics::rowSdDiffs(mat, diff = 3)) 315 | expect_equal(rowSdDiffs(sp_mat, na.rm=TRUE), MatrixGenerics::rowSdDiffs(mat, na.rm=TRUE)) 316 | expect_equal(rowSdDiffs(sp_mat, diff = 0, rows = row_subset, cols = col_subset), MatrixGenerics::rowSdDiffs(mat, diff = 0, rows = row_subset, cols = col_subset)) 317 | 318 | expect_equal(rowMadDiffs(sp_mat, diff = 0), MatrixGenerics::rowMadDiffs(mat, diff = 0)) 319 | expect_equal(rowMadDiffs(sp_mat, diff = 1), MatrixGenerics::rowMadDiffs(mat, diff = 1)) 320 | expect_equal(rowMadDiffs(sp_mat, diff = 3), MatrixGenerics::rowMadDiffs(mat, diff = 3)) 321 | expect_equal(rowMadDiffs(sp_mat, na.rm=TRUE), MatrixGenerics::rowMadDiffs(mat, na.rm=TRUE)) 322 | expect_equal(rowMadDiffs(sp_mat, diff = 0, rows = row_subset, cols = col_subset), MatrixGenerics::rowMadDiffs(mat, diff = 0, rows = row_subset, cols = col_subset)) 323 | 324 | expect_equal(rowIQRDiffs(sp_mat, diff = 0), MatrixGenerics::rowIQRDiffs(mat, diff = 0)) 325 | expect_equal(rowIQRDiffs(sp_mat, diff = 1), MatrixGenerics::rowIQRDiffs(mat, diff = 1)) 326 | expect_equal(rowIQRDiffs(sp_mat, diff = 3), MatrixGenerics::rowIQRDiffs(mat, diff = 3)) 327 | expect_equal(rowIQRDiffs(sp_mat, na.rm=TRUE), MatrixGenerics::rowIQRDiffs(mat, na.rm=TRUE)) 328 | expect_equal(rowIQRDiffs(sp_mat, diff = 0, rows = row_subset, cols = col_subset), MatrixGenerics::rowIQRDiffs(mat, diff = 0, rows = row_subset, cols = col_subset)) 329 | }) 330 | 331 | 332 | test_that("rowCollapse works", { 333 | 334 | expect_equal(rowCollapse(sp_mat, idxs = 1), MatrixGenerics::rowCollapse(mat, idxs = 1)) 335 | expect_equal(rowCollapse(sp_mat, idxs = c(1,3)), MatrixGenerics::rowCollapse(mat, idxs = c(1,3))) 336 | expect_equal(rowCollapse(sp_mat, idxs = 1:5, rows = 3), MatrixGenerics::rowCollapse(mat, idxs = 1:5, rows = 3)) 337 | expect_equal(rowCollapse(sp_mat, idxs = 1, rows = row_subset), mat[row_subset, 1]) 338 | expect_equal(rowCollapse(sp_mat, idxs = 1, rows = row_subset), MatrixGenerics::rowCollapse(mat, idxs = 1, rows = row_subset)) 339 | 340 | }) 341 | 342 | 343 | test_that("rowAvgsPerColSet works", { 344 | S <- suppressWarnings(matrix(seq_len(ncol(mat)), ncol = 2)) 345 | expect_equal(rowAvgsPerColSet(sp_mat, S = S, na.rm = TRUE), MatrixGenerics::rowAvgsPerColSet(mat, S = S, na.rm = TRUE)) 346 | expect_equal(rowAvgsPerColSet(sp_mat, S = S, FUN = rowVarDiffs, na.rm = FALSE), MatrixGenerics::rowAvgsPerColSet(mat, S = S, FUN = rowVarDiffs, na.rm = FALSE)) 347 | expect_equal(rowAvgsPerColSet(sp_mat, S = S, na.rm = FALSE, rows = row_subset), MatrixGenerics::rowAvgsPerColSet(mat, S = S, na.rm = FALSE, rows = row_subset)) 348 | }) 349 | 350 | 351 | --------------------------------------------------------------------------------