-1
a <- c(rep("A", 3), rep("B", 3), rep("C",2))
b <- c(1,1,2,4,1,1,2,2)
df <- data.frame(a,b)

But I want to keep the highest b value of "A", "B", and "C".

So my new df only has 3 rows. Thank you!!!

divibisan
  • 11,659
  • 11
  • 40
  • 58
Kevin W
  • 91
  • 2
  • 9

2 Answers2

1

This is simple with dplyr: we just group by the value of a and summarize (for each group) the max value of b:

library(dplyr)
df %>%
    group_by(a) %>%
    summarize(b = max(b))

  a         b
  <fct> <dbl>
1 A         2
2 B         4
3 C         2
divibisan
  • 11,659
  • 11
  • 40
  • 58
0

group by a and take maximum of b

  df%>%
  group_by(a)%>%
  summarise(max(b))
Nar
  • 648
  • 4
  • 8