0

I am wondering how would I select unique values in one column based on the maximum value in another column for each original distinct value.

Original data frame:

a   b   

A   3
B   4
C   5
C   8
A   11

Expected data frame:

a   b   

A   11
B   4
C   8

Thanks!

Jaap
  • 81,064
  • 34
  • 182
  • 193
MAMS
  • 419
  • 1
  • 6
  • 17

1 Answers1

1

Just do a a group by max

aggregate(b ~ a, df1, max)

If we have multiple columns in the dataset

library(dplyr)
df1 %>%
   group_by(a) %>%
   slice(which.max(b))
akrun
  • 874,273
  • 37
  • 540
  • 662