Ashlee Berry - 1 year ago 101
R Question

# How to extract top three values from 12 different columns and return the associated row name?

I am using a 43x12 data set that is built into R. The 43 rows are different people's names and the 12 columns are different stats. I need to get the people's names that scored in the top 3 for each stat. I can mostly do this except if two people have the exact same value for one stat I need to break the tie by using another stat, which I have no clue how to do. Also if they have the same value for a stat my current method doesn't work. The built in data set I'm using is

`USJudgeRatings`
. Here's how I was getting the top 3 for CONT, which works fine, but a similar method does not work for INTG as two people have the same stat which is the second highest for this category.

``````sortedCont <- sort(USJudgeRatings\$CONT, decreasing = TRUE)
ContFilter1 <- USJudgeRatings\$CONT == sortedCont[1]
ContFilter2 <- USJudgeRatings\$CONT == sortedCont[2]
ContFilter3 <- USJudgeRatings\$CONT == sortedCont[3]
BestJudges[1,1] <- rownames(USJudgeRatings[ContFilter1,])
BestJudges[1,2] <- rownames(USJudgeRatings[ContFilter2,])
BestJudges[1,3] <- rownames(USJudgeRatings[ContFilter3,])
``````

You can use `head` to get the top rows like :

``````row.names(head(USJudgeRatings[order(USJudgeRatings\$CONT,decreasing =T),],3))
[1] "CALLAHAN,R.J." "LEVISTER,R.L." "WALL,R.A."
``````

Then you can use `sapply` to apply it to each column :

``````sapply(USJudgeRatings, function(col)