Get the rownumber a variable reached the max value in a data.frame column R

i need some help to find the row number that have the max value for each column

Data = data.frame(  
                    V1 = c(0.1, 0.2, 0.4, 0.7, 0.9),
                    v2 = c(0.1, 0.12, 0.41, 0.72, 0.91),
                    v3 = c(0.03, 0.13, 0.92, 0.50, 0.90))

Desired result: (Time of max value)

  V1 V2 V3
1  5  5  3

I tried this, but without sucess:

lapply(Data, function(x) (which(max(x))))

>Solution :

Simply using dplyr::across

library(dplyr)
Data %>%
  summarise(across(everything(), ~which.max(.x)))

  V1 v2 v3
1  5  5  3

Or using sapply,

sapply(Data, function(x) which.max(x))

V1 v2 v3 
 5  5  3 

Leave a Reply