I have a long data frame with multiple visits per subject based on visits to the hospital.
| ID | VISIT_DATE | COPD | DIABETES |
|---|---|---|---|
| 1 | 2020-01-01 | 1 | 0 |
| 2 | 1965-01-01 | 0 | 0 |
| 3 | 1989-01-01 | 0 | 0 |
| 1 | 2020-02-10 | 1 | 1 |
| 2 | 1970-01-01 | 0 | 1 |
| 3 | 1995-01-01 | 1 | 1 |
I wanted to make a new variable called "VISIT_NUMBER" in which I can consecutively number which visit it is for each subject.
| ID | VISIT_DATE | COPD | DIABETES | VISIT_NUMBER |
|---|---|---|---|---|
| 1 | 2020-01-01 | 1 | 0 | 1 |
| 2 | 1965-01-01 | 0 | 0 | 1 |
| 3 | 1989-01-01 | 0 | 0 | 1 |
| 1 | 2020-02-10 | 1 | 1 | 2 |
| 2 | 1970-01-01 | 0 | 1 | 2 |
| 3 | 1995-01-01 | 1 | 1 | 2 |
I have used dplyr in the past for something like this but I am stumped on where to go next.