0

I would like to get the column name of the each row maximum. In the following simple example:

A <- c(1, 3, 2, 13, 54, 641)
B <- c(2, 1, 54, 22, 100, 3)
C <- c(1, 9, 36, 1, 104, 23)

df1 <- data.frame(A,B,C) 
df1

    A   B   C
1   1   2   1
2   3   1   9
3   2  54  36
4  13  22   1
5  54 100 104
6 641   3  23

I would like to create a column (maxValue) and get the column name of the maximum row:

    A   B   C maxValue
1   1   2   1       B
2   3   1   9       C
3   2  54  36       B
4  13  22   1       B
5  54 100 104       C
6 641   3  23       A

Any suggestions would be highly appreciated :)

Manos C
  • 333
  • 4
  • 16
  • 3
    `names(df1)[max.col(df1)]` could work I guess. Looks like a dupe to me – David Arenburg Feb 09 '16 at 08:51
  • 1
    `maxValue <- c(); for (i in 1:nrow(df1)) { maxValue <- c(maxValue, names(df1)[which(df1[i,] == max(df1[i,]))]); } newDf <- data.frame(df1, maxValue);` – Paul Feb 09 '16 at 10:10

0 Answers0