Panos Kalatzantonakis
Panos Kalatzantonakis

Reputation: 12683

Subset data frame in order to contain max value of each row and the column name

I want to subset a dataframe (df), in order to contain only the maximum value of each row for columns 1 to 10 and the name of the column.

example dataframe:

    0       1       2       3       4
    0.01    0.12    0.41    0.11    0.11
    0.13    0.12    0.33    0.14    0.07
    0.02    0.20    0.11    0.27    0.17
    0.11    0.33    0.04    0.09    0.24
    0.08    0.07    0.04    0.05    0.58

Currently I'm using this:

new_df[] <- apply(df[, 1:4], 1, max) #get the max value of current row
new_df<- subset(new_df, select = c(1)) #keep only one column

I get this:

0.41    
0.33
0.27
0.33
0.58

but I cant get the column name where the max value came from.

Desired result:

    2   0.41    
    2   0.33
    3   0.27
    1   0.33
    4   0.58

Thanks in advance for your help.

Upvotes: 5

Views: 2420

Answers (1)

Jilber Urbina
Jilber Urbina

Reputation: 61214

Try this

> t(apply(df, 1, function(x) c(which.max(x)-1, max(x))))
     [,1] [,2]
[1,]    2 0.41
[2,]    2 0.33
[3,]    3 0.27
[4,]    1 0.33
[5,]    4 0.58

Another alternative:

> t(apply(df, 1, function(x) as.numeric(c(names(which.max(x)), max(x)))))
     [,1] [,2]
[1,]    2 0.41
[2,]    2 0.33
[3,]    3 0.27
[4,]    1 0.33
[5,]    4 0.58

As suggested by DWin, another alternative is:

t(apply(df, 1, function(x) as.numeric(c(names(x)[which.max(x)], max(x)))))

Upvotes: 4

Related Questions