Skip to content
Prev 286428 / 398503 Next

which is the fastest way to make data.frame out of a three-dimensional array?

Cheat!  Arrays are stored in column major order, so you can translate
the indexing directly by:

Assume dim(yourarray) = c(n1,n2,n3)

*** warning: UNTESTED **

yourframe <- data.frame( dat = as.vector(yourarray)
 , dim1 = rep(seq_len(n1), n2*n3
,dim2 = rep( rep(seq_len(n2), e=n1), n3)
, dim3 = rep(seq_len(n3), e = n1*n2)
)

Probably see also the reshape package for more elegant solutions.

Cheers,
Bert
On Sat, Feb 25, 2012 at 7:54 AM, Hans Ekbrand <hans at sociologi.cjb.net> wrote: