title: R function to calculate mean/median of top highest values
body: <p>I have a data frame with 2 columns one with numeric values and one with a name. The name repeats itself but has different values each time. </p>
  
  <pre><code>Data &lt;- data.frame(
  Value = c(1:10),
  Name = rep(LETTERS, each=4)[1:10])
  </code></pre>
  
  <p>I would like to write a function that takes the 3 highest numbers for each name and calculates mean and median (and in case there aren’t 3 values present throw an NA) and then take all the values for each name and calculate mean and median. 
  My initial attempt looks something like this:</p>
  
  <pre><code>my.mean &lt;- function (x,y){
    top3.x  &lt;- ifelse(x &gt; 3 , NA, x)
    return(mean(top3.x), median(top3.x))
  }
  </code></pre>
  
  <p>Any hints on how to improve this will be appreciated.</p>