View source: R/balance_matrix.R
| balance_matrix | R Documentation |
Balances a matrix so that the columns and/or rows add up to a certain vector.
balance_matrix(Y, col_totals = NULL, row_totals = NULL, allow_negative = TRUE)
Y |
Matrix to be balanced. |
col_totals |
(optional) Desired sum of columns. |
row_totals |
(optional) Desired sum of rows. |
allow_negative |
Are negative entries in the balanced matrix allowed? |
Balancing is done according to the criteria of minimum sum of squares.
If neither col_totals nor row_totals is given, the same matrix will be
returned. If only one of them is given, only that axis will be
balanced.
The balanced matrix.
set.seed(2)
Y <- rnorm(3*5) |> matrix(3,5) |> round(3)
v <- c( 0.876, -1.078, 3.452, 0.261, 1.349)
h <- c(-1.851, 0.243, 6.468)
X1 <- balance_matrix(Y, v, h)
Y
X1
h
rowSums(X1)
v
colSums(X1)
X3 <- balance_matrix(Y, col_totals = v)
v
colSums(X3)
X4 <- balance_matrix(Y, row_totals = h)
h
rowSums(X4)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.