title: R Apply calculation on each row and column of data frame
body: <p>In a data frame I want to subtract the mean of each column, from every element of that column. </p>
  
  <p>Example data frame:</p>
  
  <pre><code>x &lt;- c(1,2,3,4,5,6,7,8,9,10)
  y &lt;- c(2,3,2,3,2,3,2,3,2,3)
  z &lt;- c(100,200,300,400,500,600,700,800,900,1000)
  df &lt;- data.frame(x, y, z)
  
  # get the mean of each column
  mu &lt;- colMeans(df)
  </code></pre>
  
  <p>I think I need to be using <code>sapply</code>, similar to: </p>
  
  <pre><code>df_norm &lt;- df
  df_norm[,1] &lt;- sapply(df[,1], function(x) (x-mu[1]))
  df_norm[,2] &lt;- sapply(df[,2], function(x) (x-mu[2]))
  df_norm[,3] &lt;- sapply(df[,3], function(x) (x-mu[3]))
  </code></pre>
  
  <p>but I can't figure out how to write the <code>FUN</code> argument to do this in one line of code.</p>