I would like to use the reshape2 package in R to change my long table into a wide table.
I have a data set from database which is like this (example):
id1 | id2 | info | action_time |
1 | a | info1 | time1 |
1 | a | info1 | time2 |
1 | a | info1 | time3 |
2 | b | info2 | time4 |
2 | b | info2 | time5 |
And now I want it to be like this:
id1 | id2 | info |action_time 1|action_time 2|action_time 3|
1 | a | info1 | time1 | time2 | time3 |
2 | b | info2 | time4 | time5 | |
I have tried several times and looked up some examples on some website using reshape()
or dcast()
but couldn't find such example like this. The number of action_time
for each id is different and for some of the ids they may have more than 10 action_time
s so in that case the reshaped data set will have more than 10 columns of action_time
.
Anyone can think of a handy way of doing this? If there is a way of doing this in excel(Pivot Table?) it would be awesome as well. Thank heaps
Try:
Which gives:
Data