I have a data like the following and I would like to convert it into long format.
id count a1 b1 c1 a2 b2 c2 a3 b3 c3 age
1 1 apple 2 3 orange 3 2 beer 2 1 50
1 2 orange 3 2 apple 2 2 beer 2 1 50
2 1 pear 3 2 apple 2 2 orange 2 2 45
[a1,b1,c1],[a2,b2,c2],[a3,b3,c3] are the set of three attributes that person with an assigned id is facing and this person may face multiple choice situations with count indicating the ith choice situation. I want to change it back to a long format while keep the other variables like the following:
id count a b c age
1 1 apple 2 3 50
1 1 orange 3 2 50
1 1 beer 2 1 50
1 2 orange 3 2 50
1 2 apple 2 2 50
1 2 beer 2 1 50
2 1 pear 3 2 45
2 1 apple 2 2 45
2 1 orange 2 2 45
I have tried reshape with the following commands, but I get confused in terms of where to deal with timevar and times:
l <- reshape(df,
varying = df[,3:11],
v.names = c("a","b","c"),
timevar = "choice",
times = c("a","b","c"),
direction = "long")
with the above commands, I cannot the result I want, would sincerely appreciate any help!