With your data:
A <- c("A", "a", "A", "a", "A") B <- c("A", "A", "a", "a", "a") C <- c(1, 2, 3, 1, 4)
I define a data.frame
using a combination of A and B as the key column:
AB <- paste(A, B, sep='') df <- data.frame(id=AB, C=C) > df id C 1 AA 1 2 aA 2 3 Aa 3 4 aa 1 5 Aa 4
If you need to order this data.frame
before aggregation, then:
df <- df[order(AB, decreasing=TRUE),] > df id C 1 AA 1 3 Aa 3 5 Aa 4 2 aA 2 4 aa 1
And with aggregate
you calculate the average for each id
:
meanDF <- aggregate(C~id, data=df, mean) > meanDF id C 1 aa 1.0 2 aA 2.0 3 Aa 3.5 4 AA 1.0
But if you want to order after aggregation, then:
df <- data.frame(id=AB, C=C) meanDF <- aggregate(C~id, data=df, mean) meanDF <- meanDF[order(meanDF$id, decreasing=TRUE),]