我有一个数据框如下:
dt:
Time users
14-10-2016 13:00 a
14-10-2016 13:05 b
14-10-2016 13:05 c
14-10-2016 14:20 a
14-10-2016 14:20 b
14-10-2016 14:20 a
14-10-2016 14:25 d
我想按唯一时间分配序列号,如下所示:
Time users ID
14-10-2016 13:00 a 1
14-10-2016 13:05 b 2
14-10-2016 13:05 c 2
14-10-2016 14:20 a 3
14-10-2016 14:20 b 3
14-10-2016 14:20 a 3
14-10-2016 14:25 d 4
如何在ID列中分配序列号?
答案 0 :(得分:0)
我们可以使用match
dt[, ID := match(Time, unique(Time))]
或按“时间”分组后.GRP
dt[, ID := .GRP, by = Time]
dt
# Time users ID
#1: 14-10-2016 13:00 a 1
#2: 14-10-2016 13:05 b 2
#3: 14-10-2016 13:05 c 2
#4: 14-10-2016 14:20 a 3
#5: 14-10-2016 14:20 b 3
#6: 14-10-2016 14:20 a 3
#7: 14-10-2016 14:25 d 4