Skip to content
Prev 69208 / 398506 Next

reordering a data.frame

On 5/5/05, Uwe Ligges <ligges at statistik.uni-dortmund.de> wrote:
I would probably use reshape as others have already pointed out
but just for the fun of it note that if you convert it from a data frame
to a matrix to a table to a data frame in this order:

  data.frame ==> matrix ==> table ==> data.frame

We can get the result by using matrix() followed by as.data.frame.table()
(which converts it to table and then data frame all in one).  The
one feature of this solution is that each step is relatively simple.

I assume the row and column ordering is important, given the Subject, 
but if it is not then the second line can be simplified to 
just:   as.data.frame.table(mat, resp = "VALUE")
id est VALUE
1   1   0     1
2   1   1     2
3   1   2     3
4   1   3     1
5   1   4     7
6   1   5     9
7   1   6     3
8   1   7     4
9   2   0     4
10  2   1     1
11  2   2     1
12  2   3     7
13  2   4     6
14  2   5     5
15  2   6     1
16  2   7     2