Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return first occurring 2nd largest value in data frame rows using colnames & apply

Tags:

r

Consider i have a df

> editor
          A  B  C  D  E  F  G  H  I  J
User1     1  0  5  6  5  6  5  6  2  6
User2     0  5  4  6  4  5  5  1  7  5

I want to store the column name of the first occuring 2nd largest value in above rows. Expected results

> editor
          A  B  C  D  E  F  G  H  I  J  2nd_highest
User1     1  0  5  6  5  6  5  6  2  6      C
User2     0  5  4  6  4  5  5  1  7  5      D

i tried edited$2nd_highest <- colnames(edited)[apply(edited, 1, which.max)+1] but did'nt worked well .

Any ideas ?

like image 336
Pankaj Kaundal Avatar asked Feb 27 '26 05:02

Pankaj Kaundal


1 Answers

Here's an attempt to achieve this using algebra in order to keep it vectorized and avoid by row operations (though it still does a matrix conversion similar to apply). The idea here is to find the maximum- then reduce it from the data set, then convert to log (after multiplying by -1) which will result in the largest value becoming -Inf (meaning the smallest value) and then do 1/result in order to find the largest value out of the values left.

indx <- max.col(1/log((editor - editor[cbind(1:nrow(editor), 
                max.col(editor))]) * -1), ties.method = "first")
names(editor)[indx]
# [1] "C" "D"
like image 194
David Arenburg Avatar answered Mar 01 '26 13:03

David Arenburg