Emmanuel Levy
2012-Apr-19 20:46 UTC
[R] How to "flatten" a multidimensional array into a dataframe?
Hi, I have a three dimensional array, e.g., my.array = array(0, dim=c(2,3,4), dimnames=list( d1=c("A1","A2"), d2=c("B1","B2","B3"), d3=c("C1","C2","C3","C4")) ) what I would like to get is then a dataframe: d1 d2 d3 value A1 B1 C1 0 A2 B1 C1 0 . . . A2 B3 C4 0 I'm sure there is one function to do this transformation, I just don't know which one. Thanks for your help, Emmanuel
Marc Schwartz
2012-Apr-19 20:57 UTC
[R] How to "flatten" a multidimensional array into a dataframe?
On Apr 19, 2012, at 3:46 PM, Emmanuel Levy wrote:> Hi, > > I have a three dimensional array, e.g., > > my.array = array(0, dim=c(2,3,4), dimnames=list( d1=c("A1","A2"), > d2=c("B1","B2","B3"), d3=c("C1","C2","C3","C4")) ) > > what I would like to get is then a dataframe: > > d1 d2 d3 value > A1 B1 C1 0 > A2 B1 C1 0 > . > . > . > A2 B3 C4 0 > > I'm sure there is one function to do this transformation, I just don't > know which one. > > Thanks for your help, > > EmmanuelSee ?as.data.frame.table> as.data.frame.table(my.array, responseName = "value")d1 d2 d3 value 1 A1 B1 C1 0 2 A2 B1 C1 0 3 A1 B2 C1 0 4 A2 B2 C1 0 5 A1 B3 C1 0 6 A2 B3 C1 0 7 A1 B1 C2 0 8 A2 B1 C2 0 9 A1 B2 C2 0 10 A2 B2 C2 0 11 A1 B3 C2 0 12 A2 B3 C2 0 13 A1 B1 C3 0 14 A2 B1 C3 0 15 A1 B2 C3 0 16 A2 B2 C3 0 17 A1 B3 C3 0 18 A2 B3 C3 0 19 A1 B1 C4 0 20 A2 B1 C4 0 21 A1 B2 C4 0 22 A2 B2 C4 0 23 A1 B3 C4 0 24 A2 B3 C4 0 Regards, Marc Schwartz
Emmanuel Levy
2012-Apr-19 21:00 UTC
[R] How to "flatten" a multidimensional array into a dataframe?
OK, it seems that the array2df function from arrayhelpers package does the job :) On 19 April 2012 16:46, Emmanuel Levy <emmanuel.levy at gmail.com> wrote:> Hi, > > I have a three dimensional array, e.g., > > my.array = array(0, dim=c(2,3,4), dimnames=list( d1=c("A1","A2"), > d2=c("B1","B2","B3"), d3=c("C1","C2","C3","C4")) ) > > what I would like to get is then a dataframe: > > d1 d2 d3 ?value > A1 B1 C1 0 > A2 B1 C1 0 > . > . > . > A2 B3 C4 0 > > I'm sure there is one function to do this transformation, I just don't > know which one. > > Thanks for your help, > > Emmanuel
baptiste auguie
2012-Apr-19 21:01 UTC
[R] How to "flatten" a multidimensional array into a dataframe?
library(plyr) adply(my.array,1:3) HTH, baptiste On 20 April 2012 08:46, Emmanuel Levy <emmanuel.levy at gmail.com> wrote:> Hi, > > I have a three dimensional array, e.g., > > my.array = array(0, dim=c(2,3,4), dimnames=list( d1=c("A1","A2"), > d2=c("B1","B2","B3"), d3=c("C1","C2","C3","C4")) ) > > what I would like to get is then a dataframe: > > d1 d2 d3 ?value > A1 B1 C1 0 > A2 B1 C1 0 > . > . > . > A2 B3 C4 0 > > I'm sure there is one function to do this transformation, I just don't > know which one. > > Thanks for your help, > > Emmanuel > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.