Last active
December 17, 2020 15:35
-
-
Save btupper/bbd2bf7e40c280be1d3eed55815aff71 to your computer and use it in GitHub Desktop.
Split matrices by row
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#' 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