创建报告我必须重新安排data.frame。
是:
id date kind datn
AE00049 1999-03-01 train 2
AE00049 1999-03-01 bus 2
AE00049 2009-05-01 train 1
AE00052 1997-07-15 bus 1
AE00066 2005-09-01 tram 1
AE0007X 2009-07-01 bus 2
AE0007X 2009-07-01 train 2
AE00186 2002-11-01 bus 3
AE00186 2002-11-01 train 3
AE00186 2002-11-01 tram 3
我的目标是为具有相同ID和日期的行提供新的索引号,如下所示:
id date kind datn datnno
AE00049 1999-03-01 train 2 1
AE00049 1999-03-01 bus 2 2
AE00049 2009-05-01 train 1 1
AE00052 1997-07-15 bus 1 1
AE00066 2005-09-01 tram 1 1
AE0007X 2009-07-01 bus 2 1
AE0007X 2009-07-01 train 2 2
AE00186 2002-11-01 bus 3 1
AE00186 2002-11-01 train 3 2
AE00186 2002-11-01 tram 3 3
我该怎么做?
答案 0 :(得分:3)
我们可以使用ave
使用' id'来创建序列列。和' date'作为分组变量。
df1$datnno <- with(df1, ave(seq_along(id), id, date, FUN=seq_along))