문제

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!

도움이 되었습니까?

해결책

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)
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top