1

Suppose I have a data frame like this:

 df <- data.frame(A = c(5, 5, 6, 6, 5), B = c(5, 5, 9, 9, 5), C = c(4, 1, 9, 1, 1))

A  B  C
5  5  4
5  5  1
6  9  9
6  9  1
5  5  1

If rows in the data frame have the same number in column B, then I only want to keep the row that has highest value in column C.

So according my condition, I am expected to get the filtered data frame like:

A  B  C
5  5  4
6  9  9

Thanks a lot!

Paul Wheeler
  • 18,988
  • 3
  • 28
  • 41
Wang
  • 1,314
  • 14
  • 21

2 Answers2

4

With R base aggregate

> aggregate(C~A+B, data=df, max)
  A B C
1 5 5 4
2 6 9 9
Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
3

Here is a dplyr solution:

library(dplyr)
df %>% 
  group_by(A, B) %>% 
  filter(C == max(C))
# A tibble: 2 x 3
# Groups:   A, B [2]
      A     B     C
  <dbl> <dbl> <dbl>
1     5     5     4
2     6     9     9
sbha
  • 9,802
  • 2
  • 74
  • 62