1

When performing arithmetic operations (e.g. division) on a matrix, R recycles the operand (e.g. devisor) to match the size of the matrix in a column-major fashion. Therefore, for example, if one wants to normalize matrix m by its row sums the code m/rowSums(m) will work, but for normalizing m by its column sums the code m/colSums(m) will fail!

To avoid this problem, the use of sweep() is recommended (see https://rpubs.com/bbolker/sweep_divide). To avoid the pain of having to think about this issue while programming, one might resort to using sweep() in any such matrix operation, thus

sweep(m, MARGIN=1, FUN="/", STATS=rowSums(m))   # To normalize by row sums
sweep(m, MARGIN=2, FUN="/", STATS=colSums(m))   # To normalize by column sums

That is fine for simple operations such as in the above example. However, suppose we want to write a function that performs a complex algebraic formula with numerous operations on an input matrix (including both row-wise and column-wise operations).

In that case, we would require multiple nested sweep functions combined with other operations and that can make the code quite gruesome to write, edit, and read.

An alternative could be to implement the mathematical formula for each cell of the matrix within nested row-wise and column-wise loops, but that would give up the elegance and efficiency of vectorized programming.

Is there any other coding approach that would better clarify the mathematical formula that is being implemented?

field101
  • 11
  • 3
  • Instead of philosophy, you should give a [reproducible example](https://stackoverflow.com/a/5963610/6574038). Since you seem to have already tried something, this should be an easy task. Otherwise, the question arises as to what used to be called "too broad". – jay.sf Dec 21 '21 at 13:56
  • Can't you break nested sweeps out into functions to improve readability & testing? – CJR Dec 21 '21 at 14:19

0 Answers0