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

Viewed 38

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?

2 Answers

A possible solution, using slice_max:

library(dplyr)

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

data.table method if speed is important

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