m <- matrix(c(1, 2, 3,
4, 5, 6,
7, 8, 9),
nrow = 3, ncol = 3, byrow = TRUE,
dimnames = list(c("r1", "r2", "r3"),
c("c1", "c2", "c3")))
m
# Move row 3 into the other rows (r1 and r2) proportionally
reallocate_byname(m, rowcolnames = "r3", margin = 1)
# Move column 2 into the other columns (c1 and c3) proportionally
reallocate_byname(m, rowcolnames = "c2", margin = 2)
# Demonstrate different options for reallocating when zeroes remain.
m2 <- matrix(c(1, 2, 0,
4, 5, 0,
7, 8, 10),
nrow = 3, ncol = 3, byrow = TRUE,
dimnames = list(c("r1", "r2", "r3"),
c("c1", "c2", "c3")))
m2
reallocate_byname(m2, rowcolnames = "r3", margin = 1,
.zero_behaviour = "zeroes")
reallocate_byname(m2, rowcolnames = "r3", margin = 1,
.zero_behaviour = "allocate equally")
Run the code above in your browser using DataLab