Frage

Is it possible to rank the results in the aggregate function by one column?

For example if I have this long aggregate function and want to rank(desc) by Survived:

aggregate(Survived ~ Fare2 + Pclass + Sex, data=train, FUN=function(x) {sum(x)/length(x)})

I appreciate your answer!

War es hilfreich?

Lösung

Try:

result <- aggregate(Survived ~ Fare2 + Pclass + Sex, data=train, FUN=function(x) {sum(x)/length(x)})
result <- result[order(result$Survived,decreasing=T),]

This will give you the result in descending order of Survived.

Alternatively, you could add a rank column:

result      <- aggregate(Survived ~ Fare2 + Pclass + Sex, data=train, FUN=function(x) {sum(x)/length(x)})
result$rank <- rank(result$Survived)
result      <- result[order(result$rank,decreasing=T),]

BTW: You're just calculating the mean in the function, so you could use:

aggregate(Survived ~ Fare2 + Pclass + Sex, data=train, mean)
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top