I am trying to create a new variable (v2) based on a pattern of numerical responses to another variable (v1). The dataset I am working with is in long format and ordered by visit. I have tried grouping by the 'id' variable and using various combinations of 'summarise' in dplyr, but cannot seem to figure this out. Below is an example of what I would like to achieve.
id visit v1 v2
<dbl> <int> <dbl> <int>
1 10001 1 0 1
2 10001 2 0 1
3 10002 1 0 2
4 10002 2 1 2
5 10003 1 1 3
6 10003 2 0 3
The value of 1 for v2 should reflect a response pattern of 0 across two visits for id 10001, 2 reflects a response pattern of 0/1, and so on.
Thank you in advance for the help!