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

Assign ID based on a sequence of consecutive days in R

I have a dataset with repeated measures which I want to use to assign IDs. The repeated measures are from a sequence of consecutive days. However, the sequence itself may be unbalanced (e.g., some have more days while others have less, some start with day 1 but a few others may start with 2 or 3). My question is how to create and assign the same ID withinid the same block of sequence. Here is a toy dataset:

days <- data.frame(
           day = c(1L,2L,3L,4L,5L,6L,8L,9L,10L,
                   2L,3L,4L,5L,6L,7L,9L,10L,
                   1L,2L,4L,5L,6L,8L,9L,10L,
                   1L,2L,3L,4L,5L,6L,7L,8L,9L,10L)
  )

Here is the end result I expect:

   id day
1   1   1
2   1   2
3   1   3
4   1   4
5   1   5
6   1   6
7   1   8
8   1   9
9   1  10
10  2   2
11  2   3
12  2   4
13  2   5
14  2   6
15  2   7
16  2   9
17  2  10
18  3   1
19  3   2
20  3   4
21  3   5
22  3   6
23  3   8
24  3   9
25  3  10
26  4   1
27  4   2
28  4   3
29  4   4
30  4   5
31  4   6
32  4   7
33  4   8
34  4   9
35  4  10

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

>Solution :

Get the difference between adjacent elements and check if it is less than 0, take the cumulative sum

days$id <- cumsum(c(TRUE, diff(days$day)  < 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