title: top_n versus order in r
body: <p>I am having trouble understanding the output from dplyr's top_n function. Can anybody help?</p>
  
  <pre><code>n=10
  
  df = data.frame(ref=sample(letters,n),score=rnorm(n))
  
  require(dplyr)
  
  print(dplyr::top_n(df,5,score))
  
  print(df[order(df$score,decreasing = T)[1:5],])
  </code></pre>
  
  <p>The output from <code>top_n</code> is not ordered according to score as I expected. Compare with using the <code>order</code> function</p>
  
  <pre>
   ref      score
  1   i 0.71556494
  2   p 0.04463846
  3   v 0.37290990
  4   g 1.53206194
  5   f 0.86307107
     ref      score
  7    g 1.53206194
  10   f 0.86307107
  1    i 0.71556494
  6    v 0.37290990
  4    p 0.04463846
  </pre>
  
  <p>The documentation I have read also implies the <code>top_n</code> results should be ordered by the specified column, for example</p>
  
  <p><a href="https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf" rel="noreferrer">https://www.rstudio.com/wp-content/uploads/2015/02/data-wrangling-cheatsheet.pdf</a></p>