I have a data set that looks somewhat like this
id year
1 2012
1 2014
1 2015
2 2014
2 2018
2 2019
3 2011
4 2010
I only want to keep based on these two conditions (1) one observation per id, (2) the latest year of that id. So, for example, for id 1, I only want to have the 2015 row, for id 2, I only want 2019 row, and for id 3 and 4, I only have one observation during those two years so just keep them both.
I have tried a few different things like:
df1<-subset(df, interaction(df$id, max(df$year)))
I know this subset doesn't make sense but I was just making things up, hoping that something would make sense in my head. Another one I tried,
lapply(unique(df$id), function(max) subset(df, mac(year)))
but I keep getting errors.
Any help would be greatly appreciated! Thank you in advance.