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, rownames = "r3", margin = 1)
# Move column 2 into the other columns (c1 and c3) proportionally
reallocate_byname(m, colnames = "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, rownames = "r3", margin = 1,
.zero_behaviour = "zeroes")
reallocate_byname(m2, rownames = "r3", margin = 1,
.zero_behaviour = "allocate equally")
if (FALSE) {
# "error" will cause an error to be emitted.
reallocate_byname(m2, rownames = "r3", margin = 1,
.zero_behaviour = "error")
# "warning" will cause a warning to be emitted
# and will return a result that is the same as "zeroes".
reallocate_byname(m2, rownames = "r3", margin = 1,
.zero_behaviour = "warning")
}
Run the code above in your browser using DataLab