title: How to get the 10 highest values for each group in a data frame?
body: <p>I am assuming this is an easy thing but I am unable to solve my problem. </p>
  
  <p>I have a data frame with 9 columns and I want to get the highest 3 values of the 4th column (LumenLenght) sorted for each group given in the first column.</p>
  
  <p>I would like to be able to: 
  a) find the 10 rows with the highest values for column 4 separated for each  SampleID (first column) 
  b) average the 10 values for each SampleID</p>
  
  <p><img src="https://i.stack.imgur.com/ysjla.jpg" alt="data frame"></p>
  
  <p>My current code a) sorts first the values according to SampleID and LumenLength and b) separates the highest, second highest and third highest LumenLength value per SampleID. </p>
  
  <pre><code>sorted.v= arrange(sorted.v, desc(SampleId), LumenLength)
  maxlength1 = aggregate(sorted.v$LumenLength,by = list(sorted.v$SampleId),  FUN = tail, n = 1)#highest value
  maxlength2 = aggregate(sorted.v$LumenLength,by = list(sorted.v$SampleId),  FUN = tail, n = 2)#second highest value
  maxlength3 = aggregate(sorted.v$LumenLength,by = list(sorted.v$SampleId),  FUN = tail, n = 3)#3. highest value
  </code></pre>
  
  <p>As you can see, I haven't really reached my goal jet.
  I am also pretty sure there is a better way of doing it but I  stuck right now. </p>