round_matrix: Round a matrix

View source: R/round_matrix.R

round_matrixR Documentation

Round a matrix

Description

Returns an integer matrix that preserves the rounded colSums and rowSums.

Usage

round_matrix(Y, digits = 0, MARGIN = 0)

Arguments

Y

A matrix.

digits

Decimal places to round to.

MARGIN

One of

  • 0 Preserves the rounded colSums and rowSums.

  • 1 Preserves the rounded rowSums independently of each other.

  • 2 Preserves the rounded colSums independently of each other.

Details

The function will throw a *warning* if the problem is infeasable. To be able to round the matrix in this fashion, the following things must be equal:

  • the sum of the differences between the row totals and the rounded row totals

  • the sum of the differences between the column totals and the rounded row totals

Value

The rounded matrix.

Examples

set.seed(6)
Y <- rnorm(3*5)*10 |> matrix(3,5) |> round(3)
X <- round_matrix(Y)
Y
X
colSums(Y) |> round()
colSums(X)
rowSums(Y) |> round()
rowSums(X)

cuadramelo documentation built on Oct. 10, 2024, 5:07 p.m.