我有一个时间内表面接触的数据框。我想简单地为每个ActivityID追加最后一行的副本:
head(movsdf.rbind)
ActivityID CareType HCWType Orientation Surface Date Time Dev.Date.Time SurfaceCategories
1 01 IV RN01 leftFacing AlcOutside 2019-08-03 11:08:01 2019-08-03 11:08:01 HygieneArea
2 01 IV RN01 leftFacing In 2019-08-03 11:08:12 2019-08-03 11:08:12 In
3 01 IV RN01 leftFacing Door 2019-08-03 11:08:12 2019-08-03 11:08:12 FarPatient
4 02 IV RN01 leftFacing Door 2019-08-03 11:08:18 2019-08-03 11:08:18 FarPatient
5 02 IV RN01 leftFacing Other 2019-08-03 11:08:22 2019-08-03 11:08:22 FarPatient
6 03 IV RN01 leftFacing Table 2019-08-03 11:10:26 2019-08-03 11:10:26 NearPatient
示例数据:
movsdf.rbind<-data.frame(ActivityID=rep(1:4, each=10),Surface=rep(c("In","Table","Out"),each=10))
我可以从这里找到解决方法:
repeatss <- aggregate(movsdf.rbind, by=list(movsdf.rbind$ActivityID), FUN = function(x) { last = tail(x,1) })
movsdf.rbind <-rbind(movsdf.rbind, repeatss)
这样做可以解决问题,但看起来有些笨重,而且数据不是按顺序排列的(虽然这并不是真正要紧的事情,但我感觉在dplyr
或data.table
中可能存在更优雅的方法)。有什么想法吗?
DT
来操作data.table
吗? - Jaap