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

Bert Gunter gunter.berton at gene.com
Sat Feb 25 17:07:01 CET 2012


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:
> foo <- rnorm(30*34*12)
> dim(foo) <- c(30, 34, 12)
>
> I want to make a data.frame out of this three-dimensional array. Each dimension will be a variabel (column) in the data.frame.
>
> I know how this can be done in a very slow way using for loops, like this:
>
> x <- rep(seq(from = 1, to = 30), 34)
> y <- as.vector(sapply(1:34, function(x) {rep(x, 30)}))
> month <- as.vector(sapply(1:12, function(x) {rep(x, 30*34)}))
> my.df <- data.frame(month, x=rep(x, 12), y=rep(y, 12), temp=rep(NA, 30*34*12))
> my.counter <- 1
> for(month in 1:12){
>  for(i in 1:34){
>    for(j in 1:30){
>      my.df$temp[my.counter] <- foo[j,i,month]
>      my.counter <- my.counter + 1
>    }
>  }
> }
>
> str(my.df)
> 'data.frame':   12240 obs. of  4 variables:
>  $ month: int  1 1 1 1 1 1 1 1 1 1 ...
>  $ x    : int  1 2 3 4 5 6 7 8 9 10 ...
>  $ y    : int  1 1 1 1 1 1 1 1 1 1 ...
>  $ temp : num  0.673 -1.178 0.54 0.285 -1.153 ...
>
> (In the real world problem I had, data was monthly measurements of temperature and x, y was coordinates).
>
> Does anyone care to share a faster and less ugly solution?
>
> TIA
>
> --
> Hans Ekbrand
>
> ______________________________________________
> 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.



-- 

Bert Gunter
Genentech Nonclinical Biostatistics

Internal Contact Info:
Phone: 467-7374
Website:
http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm



More information about the R-help mailing list