How to calculate the mean of the top 10% in R
Asked Answered
A

2

7

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.

I figured out how to get a given number of values (i.e., the top 20 values).

clim6 <-setDT(range)[order(species, clim6),.SD[1:20],by=species]
write.csv(Bioclimlo6, file = "clim6.csv")

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%.

mean(x, trim = 0, na.rm = FALSE)
Apostate answered 13/4, 2016 at 0:18 Comment(2)
mean(x[x >= quantile(x, 0.9, na.rm=TRUE)], na.rm=TRUE)Clearway
Why would you prefer this mean over calculating the 95th percentile of the data?Introspect
C
13

Mean of top 10% of values, using base R:

x = c(1:100,NA)
mean(x[x>=quantile(x, 0.9, na.rm=TRUE)], na.rm=TRUE)

Mean of top 10% of values, by grouping variable:

# Fake data
dat = data.frame(x=1:100, group=rep(LETTERS[1:3], c(30,30,40)))

With dplyr

library(dplyr)

dat %>% group_by(group) %>%
  summarise(meanTop10pct = mean(x[x>=quantile(x, 0.9)]))
   group meanTop10pct
  (fctr)        (dbl)
1      A         29.0
2      B         59.0
3      C         98.5

With data.table

library(data.table)

setDT(dat)[, list(meanTop10pct = mean(x[x>=quantile(x, 0.9)])), by=group] 
   group meanTop10pct
1:     A         29.0
2:     B         59.0
3:     C         98.5
Clearway answered 13/4, 2016 at 0:42 Comment(2)
Is there additionally a way to get the bottom (lowest) 10% of values for a particular variable again grouping by species?Apostate
Yes, just do x <= quantile(x, 0.1). You can choose x within any percentile ranges using the quantile function.Clearway
L
0
top10_mean <- mean(quantile(x, 0.2, na.rm = TRUE))
Lilybelle answered 13/4, 2016 at 0:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.