I am working with a dataframe in R that looks like this:
id | visit_id | value |
---|---|---|
1 | 1 | 234 |
1 | 2 | 120 |
1 | 3 | 125 |
2 | 1 | 456 |
3 | 1 | 128 |
3 | 2 | 245 |
3 | 3 | 546 |
3 | 4 | 340 |
4 | 1 | 300 |
I am trying to combine rows with the same id into one row, so that each value now becomes a separate column (ie, value1, value2). However, not every id has multiple rows and it is not always the same number of rows. If anyone has any idea how to do this in R?
id | value1 | value2 | value3 | value4 |
---|---|---|---|---|
1 | 234 | 120 | 125 | NA |
2 | 456 | NA | NA | NA |
3 | 128 | 245 | 546 | 340 |
4 | 300 | NA | NA | NA |