是否有可能以更易读的方式使用aggregate或ddply获得ret
的相同结果?
time<-c("2013-08-05 15:44:19","2013-08-05 15:44:24","2013-08-05 15:45:19","2013-08-05 15:45:28")
df<-data.frame(time=as.POSIXct(time),col2=c(1,2,2,2),col3=LETTERS[1:4])
mm<-split(df,df[,"col2"])
ret<-lapply(mm, function(x){
mt<-max(x[,"time"])
idx<-x[,"time"]==mt
x[idx,]
}
)
do.call("rbind",ret)
答案 0 :(得分:4)
使用plyr
:
R> ddply(df, "col2", summarize, time=max(time))
col2 time
1 1 2013-08-05 15:44:19
2 2 2013-08-05 15:45:28
使用data.table
:
R> dt <- data.table(df, key="col2")
R> dt[,list(time=max(time)),by=col2]
col2 time
1: 1 2013-08-05 15:44:19
2: 2 2013-08-05 15:45:28
答案 1 :(得分:3)
使用aggregate
:
> aggregate(time~col2, FUN=max, data=df)[, c(2,1)]
time col2
1 2013-08-05 15:44:19 1
2 2013-08-05 15:45:28 2
ddply
> ddply(df, .(col2), summarise, time=max(time))[, c(2,1)]
time col2
1 2013-08-05 15:44:19 1
2 2013-08-05 15:45:28 2
只是为了好玩,使用lapply
和split
> do.call(rbind, lapply(with(df, split(df, col2)),
+ function(x) x[which.max(x$time), ]))
time col2
1 2013-08-05 15:44:19 1
2 2013-08-05 15:45:28 2
<强>更新强>
最后一个解决方案适用于您的更新
> do.call(rbind, lapply(with(df, split(df, col2)),
+ function(x) x[which.max(x$time), ]))
time col2 col3
1 2013-08-05 15:44:19 1 A
2 2013-08-05 15:45:28 2 D