dat <- data.frame(year = c(1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988),
a = c(100, 200, 300, 400, 500, 350, 350, 350, 430),
p = c(60, 90, 80, 75, 95, 600, 600, 600, 330),
y = c(1, 1, 12, 11, 12, 13, 13, 13, 20))
if y
is duplicated, then only keep the y
with the highest a
.
If duplicated y
have the same a
, then keep y
with the highest p
.
If duplicated y
have the same a
and p
, then simply remove them. I did this:
dat %>% dplyr::arrange(y, a, p) %>% dplyr::group_by(y) %>%
dplyr::filter(a == max(a)) %>% dplyr::filter(p == max(p)) %>%
distinct(a, p, y)
But how do I retain my year
column here?