nsivakr
nsivakr

Reputation: 1595

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

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?

Upvotes: 1

Views: 45

Answers (2)

PaulS
PaulS

Reputation: 25323

A possible solution, using slice_max:

library(dplyr)

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

Upvotes: 3

Sweepy Dodo
Sweepy Dodo

Reputation: 1873

data.table method if speed is important

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

Upvotes: 1

Related Questions