File: utils-randomize-matrix.R

package info (click to toggle)
r-bioc-decoupler 2.12.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 2,612 kB
  • sloc: makefile: 5
file content (31 lines) | stat: -rw-r--r-- 825 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#' Randomize matrix
#'
#' Utility function used in functions that require permutations of the
#' expression matrix
#'
#' @param mat Matrix to randomize.
#' @param randomize_type How to randomize.
#'
#' @return Randomized matrix
#' @examples
#' \dontrun{
#' mat <- matrix(seq_len(9), ncol = 3)
#' mat
#'
#' set.seed(42)
#' randomize_matrix(mat, randomize_type = "rows")
#'
#' set.seed(42)
#' randomize_matrix(mat, randomize_type = "cols_independently")
#' }
#' @keywords internal
randomize_matrix <- function(mat,
                             randomize_type = c("rows", "cols_independently")) {
    randomize_type <- match.arg(randomize_type)

    switch(randomize_type,
        rows = mat[sample(nrow(mat)), , drop = FALSE],
        cols_independently = apply(mat, 2, sample)
    ) %>%
        `row.names<-`(rownames(mat))
}