这是一个data.table
解决方案,这应该是非常有效的。
DF <- data.frame(ID=c(1,2,1,3,2,1), ProjectName=c('Health', 'Education', 'Education', 'Wellness', 'Health', 'Health'),
StartDate=c('3/1/06 18:20', '2/1/07 15:30', '5/3/09 9:00', '4/1/10 12:00', '6/1/11 14:20', '1/1/06 11:10'))
请注意,我已经修改了您的数据,在末尾添加了另一个元素,因此日期不再排序。因此输出不同。
d <- as.data.table(DF)
# Order by StartDate and take the first ID.
# Assumes that your dates are month/day/year.
d[order(as.POSIXct(StartDate, format="%m/%d/%y %H:%M"))][,.SD[1,],by=ID]
## ID ProjectName StartDate
## 1: 1 Health 1/1/06 11:10
## 2: 2 Education 2/1/07 15:30
## 3: 3 Wellness 4/1/10 12:00
如果您的日期已经排序(如您的示例中所示),则这就足够了:
d[,.SD[1,],by=ID]