我的数据看起来像这样:
DF<- data.frame( id=c("A1","A2","A3","A1"), submission=c(1,1,1,2))
仅保留每个ID的最后一次提交的最佳方法是什么?那就是:
DF<- data.frame( id=c("A2","A3","A1"), submission=c(1,1,2))
谢谢!
答案 0 :(得分:5)
以下是基础R中的一些选项:
DF[!duplicated(DF$id, fromLast=TRUE),]
# id submission
# 2 A2 1
# 3 A3 1
# 4 A1 2
do.call(rbind, by(DF, DF$id, FUN=tail, 1))
# id submission
# A1 A1 2
# A2 A2 1
# A3 A3 1
aggregate(submission ~ id, DF, tail, 1)
# id submission
# 1 A1 2
# 2 A2 1
# 3 A3 1