title: How to calculate the mean of the top 10% in R
body: <p>My dataset contains multiple observations for different species. Each species has a different number of observations. Looking for a fast way in R to calculate the mean of the top 10% of values for a given variable for each species.</p>
  
  <p>I figured out how to get a given number of values (i.e., the top 20 values).  </p>
  
  <pre><code>clim6 &lt;-setDT(range)[order(species, clim6),.SD[1:20],by=species]
  write.csv(Bioclimlo6, file = "clim6.csv")
  </code></pre>
  
  <p>I also know that there is a way to trim the dataset to generate a mean of the remaining dataset but I'm not sure how to trim only the bottom 90%. </p>
  
  <pre><code>mean(x, trim = 0, na.rm = FALSE)
  </code></pre>