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
were10, 5, 7, 1, 10
. Her average should be(5+7)/2 = 6
or(5+7+10)/3 = 7.3
?– Marcus Nunes