The data set is lay out in long format and doesn't have missing value, which has 4 columns-the 1st column is "id", the 2nd column is "col", the 3rd column is binary variable "vol", and the 4th column is time. Now, I want to convert the value of "rec" in the 2nd column "col" into "rec1,rec2,rec3,..." by id.
For example, for the id from id=1 and id=6, the expected data set should look like as follows
id col vol time
1 rec1 1 1
1 rec2 1 2
1 rec3 0 3
2 rec1 1 1
2 rec2 1 2
2 rec3 1 3
3 rec1 0 1
3 rec2 0 2
3 rec3 0 3
4 rec1 1 1
4 rec2 0 2
4 rec3 0 3
5 rec1 1 1
5 rec2 0 2
6 rec1 1 1
6 rec2 1 2
6 rec3 0 3
6 rec4 0 4
The original data set is structured as follows,
structure(list(id = c(1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5,
5, 6, 6, 6, 6), col = c("rec", "rec", "rec", "rec", "rec", "rec",
"rec", "rec", "rec", "rec", "rec", "rec", "rec", "rec", "rec",
"rec", "rec", "rec"), vol = c(1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0,
0, 1, 0, 1, 1, 0, 0), time = c(1, 2, 3, 1, 2, 3, 1, 2, 3, 1,
2, 3, 1, 2, 1, 2, 3, 4)), row.names = c(NA, 18L), class = "data.frame")->df