I am new to R and I am trying to build my first regression model. However, I am struggling with transforming my data.
I have my data organized in the following format:
resp_id task_id alt A_1 B_1 C_1 D_1 E_1
1 25 1 3 0.4 0.15 0 0
1 25 2 2 0.7 0.05 0.05 0
1 26 1 1 0.4 0 0 0
1 26 2 3 0.4 0.05 0.1 0.05
I am looking for a way to transform my data from format above to the format below:
resp_id task_id alt A_1 B_1 C_1 D_1 E_1 A_2 B_2 C_2 D_2 E_2
1 25 1 3 0.4 0.15 0 0 2 0.7 0.05 0.05 0
1 26 1 1 0.4 0 0 0 3 0.4 0.05 0.1 0.05
Conceptually I understand that I need to loop through each row until we get to the column 'alt' with value 2. Then all all next column values in that row need to be copied as new columns to the row before and the row from which the values are copied needs to be deleted.
I looked at ways to get from a long dataset to a wide dataset in R, but I couldn't manage to transform my dataset to what I want.
Given my lack of programming experience, could someone help me out?