I have a data frame that contains id, POSIXct(Date & Time)
> myData
Tpt_ID Tpt_DateTime Value
1 1 2013-01-01 15:17:21 C
And without data.table...
x <- structure(list(Tpt_ID = c(1, 2, 3, 1, 2, 3, 1, 2, 3), Tpt_DateTime = structure(c(1357024641, 1357024712, 1357027202, 1357110611, 1359775112, 1359864002, 1357039041, 1359807512, 1359900002), class = c("POSIXct", "POSIXt"), tzone = ""), Value = c(10, 5, 1, 15, 6, 2, 21, 8, 3)), .Names = c("Tpt_ID", "Tpt_DateTime", "Value"), row.names = c(NA, 9L), class = "data.frame")
x$ID_Date <- paste(x$Tpt_ID,as.Date(x$Tpt_DateTime),sep="_")
f <- function(id, mydf){
tempdf <- mydf[mydf$ID_Date==id,]
return(tempdf[which.max(tempdf$Tpt_DateTime),])
}
res <- as.data.frame(t(sapply(unique(x$ID_Date), f, mydf=x)))
rownames(res) <- NULL
res