[R] How to "flatten" a multidimensional array into a dataframe?

Martin Maechler maechler at stat.math.ethz.ch
Fri Apr 20 09:04:06 CEST 2012


>>>>> baptiste auguie <baptiste.auguie at googlemail.com>
>>>>>     on Fri, 20 Apr 2012 09:01:35 +1200 writes:

    > library(plyr) 
    > adply(my.array,1:3)

Hmm,  as Marc Schwartz has already said:  
There's the base R   as.data.frame() method for "table" objects 
which also works for arrays, so

      as.data.frame(as.table(my.array))

works as well as  Marc's

      as.data.frame.table(my.array)

Do we need better ways to find this?
Martin


    > 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



More information about the R-help mailing list