0

I have a dataframe below:

A       B         C
ab     Small      1
ab     Medium     2
ab     Large      9
cd     Small     NA
cd     Medium    10
cd     Large     20

How do I aggregate this so that I take the max by group: column A and B?

I want the final result to look like this:

A          B     C
ab     Large     9
cd     Large    20

I already tried:

trial<-df %>% group_by(A, B) %>% summarise(Value = max(C))

I want to take the max of column B by group A. So for example, the maximum of column B within column A: "ab" is 9 and the value of column B is Large.

Any help would be great. Thanks!

nak5120
  • 3,410
  • 3
  • 23
  • 62
  • 4
    You are only interested of max of `C` for group `A` and you want the value of group `B` for that max. You need to express yourself correctly in order for us to help you. Try `df %>% group_by(A) %>% slice(which.max(C))` – David Arenburg Apr 13 '17 at 19:43
  • To get your desired result, you can just remove B from your group_by statement, but some of your other statements contradict that goal – Jonathan Hill Apr 13 '17 at 19:48
  • @DavidArenburg that worked great thank you, can you put this down as an answer to give you credit? – nak5120 Apr 17 '17 at 17:05
  • @NickKnauer I closed this as a duplicate, see links on top of your question. – David Arenburg Apr 17 '17 at 17:51

0 Answers0