I have a data frame that looks somewhat like this:
df <- data.frame(0:2, 1:3, 2:4, 5:7, 6:8, 2:4, 0:2, 1:3, 2:4)
colnames(df) <- rep(c(\'a\', \'b\', \
My version:
library(reshape)
as.data.frame(with(melt(df), split(value, variable)))
a b c
1 0 1 2
2 1 2 3
3 2 3 4
4 0 1 2
5 1 2 3
6 2 3 4
7 0 1 2
8 1 2 3
9 2 3 4
In the step using melt I transform the dataset:
> melt(df)
Using as id variables
variable value
1 a 0
2 a 1
3 a 2
4 b 1
5 b 2
6 b 3
7 c 2
8 c 3
9 c 4
10 a 0
11 a 1
12 a 2
13 b 1
14 b 2
15 b 3
16 c 2
17 c 3
18 c 4
19 a 0
20 a 1
21 a 2
22 b 1
23 b 2
24 b 3
25 c 2
26 c 3
27 c 4
Then I split up the value column for each unique level of variable using split:
$a
[1] 0 1 2 0 1 2 0 1 2
$b
[1] 1 2 3 1 2 3 1 2 3
$c
[1] 2 3 4 2 3 4 2 3 4
then this only needs an as.data.frame to become the data structure you need.