Home > Software engineering >  Assign ID based on a sequence of consecutive days in R
Assign ID based on a sequence of consecutive days in R

Time:04-06

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

CodePudding user response:

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))
  • Related