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!!!
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!!!
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