26
$\begingroup$

I would like to perform column-wise normalization of a matrix in R. Given a matrix m, I want to normalize each column by dividing each element by the sum of the column. One (hackish) way to do this is as follows:

m / t(replicate(nrow(m), colSums(m)))

Is there a more succinct/elegant/efficient way to achieve the same task?

$\endgroup$
41
$\begingroup$

This is what sweep and scale are for.

sweep(m, 2, colSums(m), FUN="/")
scale(m, center=FALSE, scale=colSums(m))

Alternatively, you could use recycling, but you have to transpose it twice.

t(t(m)/colSums(m))

Or you could construct the full matrix you want to divide by, like you did in your question. Here's another way you might do that.

m/colSums(m)[col(m)]

And notice also caracal's addition from the comments:

m %*% diag(1/colSums(m))
$\endgroup$
  • 8
    $\begingroup$ One more: m %*% diag(1/colSums(m)) $\endgroup$ – caracal Mar 22 '11 at 9:33
  • $\begingroup$ I've never heard of sweep function before, thanks! $\endgroup$ – Matteo De Felice Sep 14 '12 at 14:59
10
$\begingroup$

Another is prop.table(m, 2), or simply propr(m), that internally uses sweep.

It may be of interest to compare the performance of these equivalent solutions, so I did a little benchmark (using microbenchmark package).

This is the input matrix m I've used:

          [,1]         [,2]         [,3]         [,4]         [,5]
A 1.831564e-02 4.978707e-02 1.353353e-01 3.678794e-01 3.678794e-01
B 3.678794e-01 1.353353e-01 4.978707e-02 1.831564e-02 6.737947e-03
C 4.539993e-05 2.061154e-09 9.357623e-14 4.248354e-18 5.242886e-22
D 1.831564e-02 4.978707e-02 1.353353e-01 3.678794e-01 3.678794e-01
E 3.678794e-01 1.353353e-01 4.978707e-02 1.831564e-02 6.737947e-03
F 4.539993e-05 2.061154e-09 9.357623e-14 4.248354e-18 5.242886e-22
G 1.831564e-02 4.978707e-02 1.353353e-01 3.678794e-01 3.678794e-01
H 3.678794e-01 1.353353e-01 4.978707e-02 1.831564e-02 6.737947e-03
I 4.539993e-05 2.061154e-09 9.357623e-14 4.248354e-18 5.242886e-22

This is the benchmark setup:

microbenchmark(
prop = prop.table(m, 2),
scale = scale(m, center=FALSE, scale=colSums(m)),
sweep = sweep(m, 2, colSums(m), FUN="/"),
t_t_colsums = t(t(m)/colSums(m)),
m_colsums_col = m/colSums(m)[col(m)],
m_mult_diag = m %*% diag(1/colSums(m)),
times = 1500L)

This are the results of the benchmark:

Unit: microseconds
           expr     min       lq   median       uq      max
1 m_colsums_col  29.089  32.9565  35.9870  37.5215 1547.972
2   m_mult_diag  43.278  47.6115  51.7075  53.8945  110.560
3          prop 207.070 214.3010 216.6800 219.9680 2091.913
4         scale 133.659 142.6325 145.3100 147.9195 1730.640
5         sweep 113.969 119.6315 121.3725 123.6570 1663.356
6   t_t_colsums  56.976  65.3580  67.8895  69.5130 1640.660

For completeness, this is the output:

          [,1]         [,2]         [,3]         [,4]         [,5]
A 1.580677e-02 8.964714e-02 2.436862e-01 3.175247e-01 3.273379e-01
B 3.174874e-01 2.436862e-01 8.964714e-02 1.580862e-02 5.995403e-03
C 3.918106e-05 3.711336e-09 1.684944e-13 3.666847e-18 4.665103e-22
D 1.580677e-02 8.964714e-02 2.436862e-01 3.175247e-01 3.273379e-01
E 3.174874e-01 2.436862e-01 8.964714e-02 1.580862e-02 5.995403e-03
F 3.918106e-05 3.711336e-09 1.684944e-13 3.666847e-18 4.665103e-22
G 1.580677e-02 8.964714e-02 2.436862e-01 3.175247e-01 3.273379e-01
H 3.174874e-01 2.436862e-01 8.964714e-02 1.580862e-02 5.995403e-03
I 3.918106e-05 3.711336e-09 1.684944e-13 3.666847e-18 4.665103e-22

With no doubts for little matrices m / colSums(m)[col(m)] wins!


But for big matrices? In the subsequent example I've used a 1000x1000 matrix.

set.seed(42)
m <- matrix(sample(1:10, 1e6, TRUE), 1e3)
...
Unit: milliseconds
           expr      min       lq   median        uq       max
1 m_colsums_col 55.26442 58.94281 64.41691 102.69683 119.08685
2   m_mult_diag 34.67692 41.68494 80.05480  89.48099  99.72062
3          prop 87.95552 94.13143 99.17044 136.03669 160.51586
4         scale 52.84534 55.07107 60.57154  99.87761 156.16622
5         sweep 52.79542 55.93877 61.55066  99.67766 119.05134
6   t_t_colsums 63.09783 65.53783 68.93731 110.03691 127.89792

For big matrices m / colSums(m)[col(m)] performs well (4th position) but does not win.

For big matrices m %*% diag(1/colSums(m)) wins!

$\endgroup$
5
$\begingroup$
apply(m,2,norm<-function(x){return (x/sum(x)}) ?
$\endgroup$
  • 4
    $\begingroup$ Welcome to the site, @Sowmyalyer. Would you mind adding some text to introduce & explain your answer more fully? $\endgroup$ – gung - Reinstate Monica May 13 '13 at 18:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.