Skip to content

Instantly share code, notes, and snippets.

@btupper
Last active December 17, 2020 15:35
Show Gist options
  • Save btupper/bbd2bf7e40c280be1d3eed55815aff71 to your computer and use it in GitHub Desktop.
Save btupper/bbd2bf7e40c280be1d3eed55815aff71 to your computer and use it in GitHub Desktop.
Split matrices by row
#' Split a matrix by row much as is done for \code{data.frame}
#'
#' @param x the matrix
#' @param f a ‘factor’ see \code{split}
#' @param ... other arguments passed to \code{split}
#' @return a list of matrices
split_matrix <- function(x, f, ...){
rnm <- rownames(x)
if (is.null(rnm)){
rnm <- seq_len(nrow(x))
}
ix <- split(rnm, f, ...)
lapply(ix,
function(i, data = NULL){
data[i,]
}, data = x)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment