R/sparse.R

Defines functions extract_row_dgRMatrix extract_row_lgRMatrix

Documented in extract_row_dgRMatrix extract_row_lgRMatrix

#' Returns a row of a sparse matrix of class lgRMatrix.
#' Function made by Ben Bolker and Ott Toomet (see https://stackoverflow.com/questions/47997184/)
#'
#' @param m a sparse matrix of class lgRMatrix
#' @param i the index of the row to return
#'
#' @return A row (logical vector) of the sparse matrix
extract_row_lgRMatrix <- function(m, i = 1) {
  r <- logical(ncol(m)) ## set up vector with FALSE values for results
  inds <- seq(
    from = m@p[i] + 1,
    to = m@p[i + 1],
    length.out = max(0, m@p[i + 1] - m@p[i])
  )
  r[m@j[inds] + 1] <- m@x[inds] ## set values
  return(r)
}

#' Returns a row of a sparse matrix of class dgRMatrix.
#' Function made by Ben Bolker and Ott Toomet (see https://stackoverflow.com/questions/47997184/)
#'
#' @param m a sparse matrix of class dgRMatrix
#' @param i the index of the row to return
#'
#' @return A row (numerical vector) of the sparse matrix
extract_row_dgRMatrix <- function(m, i = 1) {
  r <- numeric(ncol(m)) ## set up vector with zero values for results
  inds <- seq(
    from = m@p[i] + 1,
    to = m@p[i + 1],
    length.out = max(0, m@p[i + 1] - m@p[i])
  )
  r[m@j[inds] + 1] <- m@x[inds] ## set values
  return(r)
}

Try the singleCellHaystack package in your browser

Any scripts or data that you put into this service are public.

singleCellHaystack documentation built on March 28, 2021, 9:12 a.m.