I've read through a number of similar posts and tutorials but am really struggling to understand the solution to my issue. I have a dataset that is wide, and when I make it longer - I want to collapse two sets of data (both duration and results).
For each participant (id
), there is a category, and then a series of blood test results. Each test has both duration (in days) and a result (numeric value).
Here's how it looks now:
id | category | duration_1 | results_1 | duration_2 | results_2 | duration_3 | results_3 |
---|---|---|---|---|---|---|---|
01 | diabetic | 58 | 32 | 65 | 56 | 76 | 87 |
02 | prediabetic | 54 | 32 | 65 | 25 | 76 | 35 |
03 | unknown | 46 | 65 | 65 | 56 | 21 | 67 |
How I'd like it to be is:
id | category | duration | results |
---|---|---|---|
01 | diabetic | 58 | 32 |
01 | diabetic | 65 | 56 |
01 | diabetic | 76 | 87 |
02 | prediabetic | 54 | 32 |
02 | prediabetic | 65 | 25 |
02 | prediabetic | 76 | 35 |
03 | unknown | 46 | 65 |
03 | unknown | 65 | 25 |
03 | unknown | 21 | 67 |
I can get pivot longer to work for "results" - but I can't get it to pivot on both "results" and "duration".
Any assistance would be greatly appreciated. I'm still fairly new to R. Thanks!