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

Hans Ekbrand hans at sociologi.cjb.net
Sat Feb 25 16:54:30 CET 2012

```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

```