I am seeking help to combine rows into one under a certain ID. The conditions are as below:
1) when gap between end_date and start_date is within 1 - 3 then they will combine into one.
2) Under the same ID if any of the variables between the rows differ then time gap conditions won't apply and they won't combine
For example, considering the table below under: Id 1 #, all the variables (i.e., x,y,z) are same. end_date = 13 in the first row and start_date = 16 in the second row are combined and makes it start_date = 10 and end_date = 18.
Under Id 3 # since the variable z varies (i.e., ccc and asd) between the two rows, thus even though the gap between star_date and end_date is 2 (i.e., end_date = 85, start_date = 87), but it didn't combine
Dataset:
Id | start_date | end_date | x | y | z |
1 | 10 | 13 | 12 | 10 | aaa |
1 | 16 | 18 | 12 | 10 | aaa |
1 | 25 | 22 | 12 | 10 | aaa |
1 | 24 | 30 | 13 | 14 | aaa |
2 | 11 | 20 | 44 | 50 | xxx |
2 | 21 | 34 | 44 | 50 | xxx |
2 | 40 | 45 | 44 | 50 | xxx |
3 | 78 | 85 | 25 | 31 | ccc |
3 | 87 | 90 | 25 | 31 | asd |
3 | 92 | 93 | 25 | 31 | pqs |
3 | 94 | 99 | 29 | 34 | pqs |
Result:
Id | start_date | end_date | x | y | z |
1 | 10 | 18 | 12 | 10 | aaa |
1 | 25 | 30 | 13 | 14 | aaa |
2 | 11 | 34 | 44 | 50 | xxx |
2 | 40 | 45 | 44 | 50 | xxx |
3 | 78 | 85 | 25 | 31 | ccc |
3 | 87 | 90 | 25 | 31 | asd |
3 | 92 | 99 | 29 | 34 | pqs |
Thank you in advance.
Kind regards,
Rakib
0 Response to combine observations under conditions
Post a Comment