Average by classes after excluding MIN and MAX

Asked

Viewed 49 times

4

I found some similar questions, but none with the same problem I’m facing.

I need to calculate the average by class, but before calculating each average, I would like to exclude the MAX and MIN values of each class:

EXAMPLE:

classe <- rep(c("A", "B"), each = 4)
valor <- c(10, 5, 7, 1, 19, 4, 2, 0)

dados <- data.frame(classe, valor)
  classe valor
1      A    10
2      A     5
3      A     7
4      A     1
5      B    19
6      B     4
7      B     2
8      B     0

In this case, for class A, the values 10(max) and 1(min) would be excluded and then the mean would be calculated: (5+7)/2 = 6.

To simply calculate the average by classes, I am using the following code:

aggregate(empresas$pL, list(setor = empresas$setor), mean)

But I still need to figure out how to delete the Maxs and Mins

  • If there are any classes with repeated maximum or minimum values, do you need to exclude both? For example, suppose the class A were 10, 5, 7, 1, 10. Her average should be (5+7)/2 = 6 or (5+7+10)/3 = 7.3?

1 answer

3

Here are two ways to do what the question asks. The trick is to use range to obtain at once the values of min and of max.

tapply(dados$valor, dados$classe, function(x){
  mean(x[!x %in% range(x)], na.rm = TRUE)
})
#A B 
#6 3 

aggregate(valor ~ classe, dados, FUN = function(x){
  mean(x[!x %in% range(x)], na.rm = TRUE)
})
#  classe valor
#1      A     6
#2      B     3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.