shuffle: Shuffle matrix elements by row or column

View source: R/sparse_matrix_functions.R

shuffleR Documentation

Shuffle matrix elements by row or column

Description

Re-orders the elements in each row or column of the matrix. Useful for defining null distributions. In the case of large sparse matrices (class dgTMatrix, dgCMatrix or dgRMatrix), this is faster than using apply, although for small matrices it may be slower.

Usage

shuffle(mat, MARGIN = 2)

Arguments

mat

A matrix. May be sparse (i.e. of class dgTMatrix, dgCMatrix or dgRMatrix).

MARGIN

The subscript to apply over. Possible values are 1 and 2. If 1, the rows of the matrix are re-ordered; if 2, the columns.

Value

A matrix of the same size and class as mat.

Examples

mat <- matrix(round(runif(100, max = 100)), 10, 10)
shuffle(mat)
shuffle(mat, MARGIN = 1)
mat[sample(1:100, 80)] <- 0
mat <- as(mat, 'dgCMatrix')
shuffle(mat)

m20ty/matkot documentation built on July 1, 2023, 8:11 p.m.