title: Dividing columns by colSums in R
body: <p>I am trying to scale the values in a matrix so that each column adds up to one. I have tried:</p>
  
  <pre><code>m = matrix(c(1:9),nrow=3, ncol=3, byrow=T)
       [,1] [,2] [,3]
  [1,]    1    2    3
  [2,]    4    5    6
  [3,]    7    8    9
  
  colSums(m)
  12 15 18
  
  m = m/colSums(m)
            [,1]      [,2] [,3]
  [1,] 0.08333333 0.1666667 0.25
  [2,] 0.26666667 0.3333333 0.40
  [3,] 0.38888889 0.4444444 0.50
  
  colSums(m)
  [1] 0.7388889 0.9444444 1.1500000
  </code></pre>
  
  <p>so obviously this doesn't work.
  I then tried this:</p>
  
  <pre><code>m = m/matrix(rep(colSums(m),3), nrow=3, ncol=3, byrow=T)
            [,1]      [,2]      [,3]
  [1,] 0.08333333 0.1333333 0.1666667
  [2,] 0.33333333 0.3333333 0.3333333
  [3,] 0.58333333 0.5333333 0.5000000
  
   m = colSums(m)
  [1] 1 1 1
  </code></pre>
  
  <p>so this works, but it feels like I'm missing something here. This can't be how it is routinely done. I'm certain I am being stupid here.
  Any help you can give would be appreciated
  Cheers,
  Davy</p>