Given the first data frame consisting of 4 columns
libId studId year freq
1000 3 2002 3
1000 34 2002 2
1000 52 2004 9
1001 17 2003 5
1001 34 2003 1
.
.
.
.
If I were to extract only data with maximum frequency based on year for each libId this would be the desired output.
libId studId year freq
1000 3 2002 3
1000 52 2004 9
1001 17 2003 5
.
.
.
.
So far I've tried
data2<- by(data1, c(data1[c(1,2,3)]), function(df) max(df$Freq))
data2 <- as.data.frame(as.table(data2))
But my output is incorrect:
libId studId year freq
1000 3 2002 3
1000 34 2002 2
1000 52 2004 9
1001 17 2003 5
1001 34 2003 1
.
.
.
.