Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading