I have a data frame that looks like this:
ID X1 X2 X3 X4 X5 X6 X7 X8
202 1 0 895 114 17 21 1 4
202 2 0 130 399 74 19 4 4
202 3 0 364 112 48 12 5 4
202 4 4 104 012 83 81 0 4
203 1 0 895 112 76 49 1 5
203 2 2 950 815 32 35 4 5
203 3 0 3.4 156 69 14 5 5
203 4 0 868 025 71 20 0 5
204 2 0 801 398 51 44 4 8
204 4 4 205 000 14 24 0 8
I want to put data corresponding to an ID in one row. For an ID, there are different numbers of rows which are mentioned by "X1" column, "X8" column is same for an ID, and "X2" contains only one non-zero value and I am interested only in this value. If values are not available for new columns, it can be set to 999. So, I want to finally look like:
ID X8 X2 X3_1 X4_1 X5_1 X6_1 X7_1 X3_2 X4_2 X5_2 X6_2 X7_2 X3_3 X4_3 X5_3 X6_3 X7_3 X3_4 X4_4 X5_4 X6_4 X7_4
202 4 4 895 114 17 21 1 130 399 74 19 4 364 112 48 12 5 104 12 83 81 0
203 5 2 895 112 76 49 1 950 815 32 35 4 3.4 156 69 14 5 868 25 71 20 0
204 8 4 999 999 999 999 999 801 398 51 44 4 999 999 999 999 999 205 0 14 24 0
I wish to use R for this. Thanks in advance for the help.