Home > OS >  Find the maximum value for each group in dataframe and retrieve the entire row
Find the maximum value for each group in dataframe and retrieve the entire row

Time:02-08

I've the following dataframe (With many records) and would like to retrieve the entire row for each day with maximum differences.

Table

require(dplyr)
# This gets the maximum value for each date
maxInfo = results %>% group_by(t) %>% summarise(Value = max(differences))

I'm able to get the max value for each day but how to get the entire row?

CodePudding user response:

A possible solution, using slice_max:

library(dplyr)

results %>% group_by(t) %>% slice_max(differences)

CodePudding user response:

data.table method if speed is important

results[ results[, .I[ differences == max(differences )], .(t)]$V1 ]
  •  Tags:  
  • Related