0

I have some coordinates recorded, stored in a format like so. The problem is that the coordinates are not recorded from t = 0 (although that would be great if they could be, but I guess that could be fixed with aggregate() after IDs have been appended).

So, every time a new set of coordinates come in you can see that the timing "drops", as the timings can overlap in the original data.

I'd like to have an ID N for every t to t+1, 2, 3, ... n increasing by 1 until the timing hits a new track and consequently doesn't fit an increase in t by 1.

How can I do that?

> tail(df, n = 30)
       t                  x                  y
 1: 1007 27.029231497274324  39.28568982361215  # Begin N
 2: 1008 27.141171214231765   39.2680792135878
 3: 1009 27.132248071463955 39.354005741468555
 4: 1010 27.133331350182065  39.38162291007978
 5: 1011  26.96051419980979 39.467267426776935
 6: 1012  27.10190720872747 39.357335994662634
 7: 1013  27.03330912545623  39.55474746907788
 8: 1014  26.94671506418284  39.45584925316438
 9: 1015 27.177393868722078 39.265825631380736
10: 1016 27.054294195073545    39.386883081807
11: 1017 27.065580771631943  39.37043708483576
12: 1018 27.160213996686252  39.34035201610493
13: 1019 27.119810899324158  39.34272069201823  # End N
14: 1003  6.983956050893645 15.086929648647404  # Begin N+1 as t != 1020, and therefore a new series
15: 1004  7.055010204697514   15.1990781811743
16: 1005  7.128298882909985 15.309147954424141
17: 1006  7.135393797970623  15.30268639426722
18: 1007  7.263759648053052 15.479778536529446
19: 1008  6.841424937548139  14.91384883555223
20: 1009  7.012375733456416 15.172494398341703
21: 1010  7.057699880715402 15.167729122504943
22: 1011 7.1578121321620625 15.182831657628816
23: 1012  7.123692460276374 15.190575721699304
24: 1013    6.9843380530963 15.202601076699773
25: 1014  6.984727322651716 15.007235971971005
26: 1015  7.039796871985575  15.12950384872048
27: 1016 7.0115158526808585  15.16191515066389
28: 1017 6.9185697622220586 15.178533360345453
29: 1018  7.069924477151925 15.117934130162984
30: 1019  7.082619461727997 15.279920311107995
komodovaran_
  • 1,940
  • 16
  • 44
  • I believe the `cumsum(...diff(...` idiom may be useful here. This might be a canonical Q&A: [How to partition a vector into groups of regular, consecutive sequences?](http://stackoverflow.com/questions/5222061/how-to-partition-a-vector-into-groups-of-regular-consecutive-sequences). – Henrik May 08 '17 at 09:39
  • 4
    try `df$id <- cumsum(c(1, diff(df$t) != 1))` – Sotos May 08 '17 at 09:39
  • Nice and simple! – komodovaran_ May 08 '17 at 09:58
  • 1
    `df[, ID:=cumsum(c(1, diff(t) != 1))]` for `data.table` – jogo May 08 '17 at 11:34

0 Answers0