I have a dataframe consisting of surveys that are done for specific subjects on specific days. Looking something like this:
| subject_id | day | data |
|---|---|---|
| 01 | 1 | 34 |
| 02 | 1 | 54 |
| 03 | 3 | 55 |
| 04 | 4 | 56 |
However, sometimes the survey is done two times in a day, resulting in duplicate Subject_id and Day values, but different data (in reality the data spans multiple columns).
| subject_id | day | data |
|---|---|---|
| 01 | 1 | 34 |
| 01 | 1 | 58 |
| 02 | 1 | 54 |
| 03 | 3 | 55 |
| 04 | 4 | 56 |
The dataframe is sorted by time, such that the second row represents the second survey on day 1 for subject 1. What I would like to do is to add a column called name that adds "- 2" to every second duplicate subject_id and day and adds nothing (or "- 1" is also acceptable) for non duplicates. In the end the data should like this.
| subject_id | day | data | name |
|---|---|---|---|
| 01 | 1 | 34 | 1 |
| 01 | 1 | 58 | 1 -2 |
| 02 | 1 | 54 | 1 |
| 03 | 3 | 55 | 3 |
| 04 | 4 | 56 | 4 |
I have tried using dplyr and janitor to mark the duplicates. However after many hours I still failed to make the new column as described.