0

是否有可能以ret更易读的方式获得与聚合或 ddply 相同的结果?

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)
4

2 回答 2

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
于 2013-09-19T13:43:44.383 回答
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
于 2013-09-19T13:42:23.507 回答