Skip to content
Prev 300121 / 398506 Next

Arrange two columns into a five variable dataframe

On 7/13/2012 8:37 PM, darnold wrote:
Generally I would recommend either the reshape function or the functions 
in the reshape2 package. However, your data doesn't quite have what is 
needed to use those. You are implicitly assuming that the first 
occurring values in each group go together (should be in the same row), 
the second ones, etc.  The reshapes require an explicit indication of 
which variables go together.

The unstack function will work for you and uses the same assumption.

 > unstack(flies)
    Group.5 Group.4 Group.3 Group.2 Group.1
1       16      35      21      46      40
2       19      37      40      42      37
3       19      49      44      65      44
4       32      46      54      46      47
5       33      63      36      58      47
6       33      39      40      42      47
7       30      46      56      48      68
8       42      56      60      58      47
9       42      63      48      50      54
10      33      65      53      80      61
11      26      56      60      63      71
12      30      65      60      65      75
13      40      70      65      70      89
14      54      63      68      70      58
15      34      65      60      72      59
16      34      70      81      97      62
17      47      77      81      46      79
18      47      81      48      56      96
19      42      86      48      70      58
20      47      70      56      70      62
21      54      70      68      72      70
22      54      77      75      76      72
23      56      77      81      90      74
24      60      81      48      76      96
25      44      77      68      92      75