[R] which is the fastest way to make data.frame out of a three-dimensional array?
Petr Savicky
savicky at cs.cas.cz
Sat Feb 25 18:40:12 CET 2012
On Sat, Feb 25, 2012 at 08:07:01AM -0800, Bert Gunter wrote:
> 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)
> )
Hi.
Try this
df <- data.frame(dat=c(foo), which(foo == foo, arr.ind=TRUE))
This may be less efficient, but easier to remember.
Hope this helps.
Petr Savicky.
More information about the R-help
mailing list