I have grouped data in R using the aggregate method.
Avg=aggregate(x$a, by=list(x$b,x$c),FUN= mean)
This gives me the mean for all the values of 'a' grouped by 'b' and 'c' of data frame 'x'.
Now, instead of taking the average of all values of 'a', I want to take the average of 3 maximum values of 'a' grouped by 'b' and 'c' .
Sample data set
a b c
10 G 3
20 G 3
22 G 3
10 G 3
15 G 3
25 G 3
30 G 3
After the above Aggregate function, it will give me:
Group.1 Group.2 x
G 3 18.85
But I want to take just the maximum 5 values of 'a' for the average
Group.1 Group.2 x
G 3 22.40
I am not able to accommodate the below maximum function that I am using in the Aggregate function
index <- order(vector, decreasing = T)[1:5]
vector(index)
Can anyone please throw some light on if this is possible?