Recode id in different time spans

I have longitudinal data on the same individuals across different time spans. How do I assign a new id to the same individual when the time is reset.

Here is an example dataset:

df <- data.frame(time=c(1,2,3,4,2,3,4,5,6,3,4),
                 id=c(1,1,1,1,1,1,1,1,1,1,1))
df
   time id
      1  1
      2  1
      3  1
      4  1
      2  1
      3  1
      4  1
      5  1
      6  1
      3  1
      4  1

The expected result is this:

  time id
      1  1
      2  1
      3  1
      4  1
      2  2
      3  2
      4  2
      5  2
      6  2
      3  3
      4  3

>Solution :

Assuming that the time column is always in order. With Base R,

cumsum(c(1,diff(df[,"time"])<0))

gives,

[1] 1 1 1 1 2 2 2 2 2 3 3

Of course you can assign it to the id column like,

df[,"id"] <-  cumsum(c(1,diff(df[,"time"])<0))

Leave a Reply