Ashlee Berry 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

. 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,])

Answer Source

You can use head to get the top rows like :

row.names(head(USJudgeRatings[order(USJudgeRatings$CONT,decreasing =T),],3))

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

sapply(USJudgeRatings, function(col) 
       row.names(head(USJudgeRatings[order(col,decreasing =T),],3)))

     CONT            INTG              DMNR            DILG           CFMG            DECI           PREP          
[3,] "WALL,R.A."     "O'SULLIVAN,T.J." "DALY,J.J."     "BERDON,R.I."  "CALLAHAN,R.J." "BERDON,R.I."  "BERDON,R.I." 
     FAMI           ORAL            WRIT           PHYS            RTEN           
[2,] "NARUK,H.J."   "NARUK,H.J."    "NARUK,H.J."   "NARUK,H.J."    "CALLAHAN,R.J."
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download