title: R help: keep top n entries per column
body: <p>Again I'm stuck on matrix manipulations that could probably solved with apply or a loop.<br>
  I have a large data frame from which I would like to keep a certain number n of the highest values per column. But the dimensions of the data frame should not be changed. So I thought it's a good idea do it like that:<br>
  1. Find the nth highest number per column with (in this case: third highest, figured out with help of other posts)</p>
  
  <pre><code>x &lt;- c(0.2, 0.23, 0.35, 0.56, 0.12, 0.7, 0, 0.66, 0.45, 0.21, 0.49, 0.47, 0.1, 0.63, 0.55)
  m &lt;- matrix(data = x, nrow=4, ncol = 4)
  &gt;m
       [,1] [,2] [,3] [,4]
  [1,] 0.20 0.12 0.45 0.47
  [2,] 0.23 0.70 0.21 0.10
  [3,] 0.35 0.00 0.49 0.63
  [4,] 0.56 0.66 0.43 0.55
  
  m.low &lt;- apply(m, 2, function(i) sort(i)[ dim(m)[1]-2])
  </code></pre>
  
  <p>2. replace all values below the one in m.low with 0, done per column. This is where I don't know how to go on... 
  The desired output should look something like this: </p>
  
  <pre><code> &gt; m.new
       [,1] [,2] [,3] [,4]
  [1,] 0.00 0.12 0.45 0.47
  [2,] 0.23 0.70 0.00 0.00
  [3,] 0.35 0.00 0.49 0.63
  [4,] 0.56 0.66 0.43 0.55
  </code></pre>
  
  <p>I would really appreciate if someone could help me and add some explanation of the function to the post. Also, there might be an easier way without finding the nth value first.
  Thanks!  </p>
  
  <p>*Edited m for better reproducibility and added m.new as desired output. Sorry! </p>