Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to find the top N values by group or within category (groupwise) in an R data.frame
    text
    copied!<p>my apologies if this is a duplicate. this seems like a question that SO would've answered long ago, but i did quite a bit of searching and couldn't find anything specifically answering this. there are lots of related questions that could be used to answer this, but i figured it should be answered formally.</p> <p>this is in response to <a href="http://r.789695.n4.nabble.com/top-10-n-values-for-each-classes-tt4658074.html#a4658095" rel="noreferrer">this question asked on the r-help mailing list</a>.</p> <p><a href="https://stackoverflow.com/search?q=top+value+by+group">here are <em>lots</em> of examples</a> of how to do this using <code>sql</code>, so i imagine it's easy to convert that knowledge over using the R <code>sqldf</code> package.. but there are a few ways to do this with R, and i wanted to check if others had ideas.</p> <p>main question: using the example <code>mtcars</code> data.frame, how would someone find the top or bottom (maximum or minimum) N records within a stated category? the top or bottom N results, per group.</p> <p>if you open up R and type <code>mtcars</code> you get an example table with 32 records. when grouped by the cylinder column <code>cyl</code> - here are the top three records for each distinct value of <code>cyl</code>. note that ties are excluded in this case, but it'd be nice to show some different ways to treat ties.</p> <pre><code> mpg cyl disp hp drat wt qsec vs am gear carb ranks Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 2.0 Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 1.0 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 2.0 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 3.0 Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 1.0 Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 1.5 Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 1.5 Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 3.0 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload