Skip to content
Prev 282630 / 398498 Next

Extracting rows with latest date from a data frame

Hi
dataframe
against
are
df2<-data.frame(uID2=c("1","1","1","2","2","2"),Date=c(as.Date("20/12/2010",
as.Date("12/07/2010",format="%d/%m/%Y"),as.Date("12/05/2009",format="%d/%m/%Y"),
new
not
and
latest <- aggregate(df2$Date, list(df2$uID2), max)

gives you newest date from df2 for each id. After that you can select rows 
from df2.

df2[df2$Date %in% latest$x,]
  uID2       Date
3    1 2011-07-12
6    2 2012-10-20

Or you can use ?merge

Regards
Petr
http://www.R-project.org/posting-guide.html