1

I've got a table with n columns. I'd like to find the largest value in each column, and then return the value from column CAG that's in the same row. I've managed to do this for column A01, but can you help me apply across all columns?

#My data frame
data <- data.frame(CAG = c(13, 14, 15), A01 = c(6485,35,132), A02 = c(0,42,56))

#My function# applied to work with column A01
result <- data$CAG[data$'A01' == max(data$'A01')]
Mike
  • 921
  • 7
  • 26
  • Possible duplicate of [for each row, return the column name of the largest value](https://stackoverflow.com/questions/17735859/for-each-row-return-the-column-name-of-the-largest-value) – lebelinoz Jun 23 '17 at 06:04

1 Answers1

1

You want which.max. Apply it to all columns using sapply.

> data[sapply(data[2:ncol(data)], which.max), ]$CAG
[1] 13 15
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197