Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading