Reputation: 12707
This should be simple but for some reason I'm stuck. I have data that looks like
A <- data.frame(x=rnorm(10), y=rnorm(10), z=rnorm(1))
B <- data.frame(x=rnorm(10), y=rnorm(10), z=rnorm(1))
C <- data.frame(x=rnorm(10), y=rnorm(10), z=rnorm(1))
method1 <- list(A=A,B=B,C=C)
method2 <- list(A=A,B=B,C=C)
biglist <- list(method1, method2)
And I'd like to reformat the data to look like
x y z dataset method
- - - --- -----
1 1 1 A 1
1 0 2 A 1
1 0 3 A 1
1 1 3 A 1
1 1 1 B 1
1 0 2 B 1
1 0 3 B 1
1 1 3 B 1
1 1 1 C 1
1 0 2 C 1
1 0 3 C 1
1 1 3 C 1
1 1 1 A 2
1 0 2 A 2
1 0 3 A 2
1 1 3 A 2
1 1 1 B 2
1 0 2 B 2
...
melt doesn't quite do what I want because it collapses my x/y/z variable headings.
Upvotes: 4
Views: 2175
Reputation: 32371
Your data.frames should have a primary key,
e.g., an identifier column, or a column with unique values --
otherwise, you will end up with the x, y, z
coordinates of a point in several unrelated rows.
I will assume that x
is such a column.
You may also have to call dcast
after melt
,
if the resulting data.frame is too vertical.
library(reshape2)
d <- melt(biglist, id.vars="x")
d <- dcast( d, L1 + L2 + x ~ variable )
Upvotes: 6