id = c(1,2,1,4,2)
year = c(1980, 1990, 2000, 2010, 2020)
salary = c(2000, 3000, 4000, 5000, 6000)
df = data.frame(id, year, salary);
df
# id year salary
# 1 1 1980 2000
# 2 2 1990 3000
# 3 1 2000 4000
# 4 4 2010 5000
# 5 2 2020 6000
df %>% group_by(id) %>%
arrange(id, desc(year)) %>%
filter(row_number() == 1) %>%
ungroup()
# id year salary
# <dbl> <dbl> <dbl>
# 1 2000 4000
# 2 2020 6000
# 4 2010 5000
df %>% group_by(id) %>% #按id分组
arrange(id, desc(year)) %>% #按id升序,year降序
filter(row_number() == 1) %>% #取分组后的第一条记录,即日期最近的一条记录
ungroup() #取消分组