I have a large 3D array that I want to write as a single column to a file. The data needs to vary first by Z (high to low) then by x (low to high) then by y (low to high). At the moment I am doing this by:
arr <<- array(0,dim=c(x,y,z)) # values aren't really 0
dataf <- data.frame(Px=rep(0,x*y*z))
ticker <- 0
for (j in 1:y){
for (i in 1:x){
for (k in z:1){
ticker <- ticker +1
dataf[ticker,1] <- arr[i,j,k]
}
}
}
write.table(dataf,file=filename,row.names=F,col.names=F)
This process is slow and seems to slow down as the iterations progress (seen with a progress bar). I'm sure theres a way to use adply but I can't get it to work. I switched the order of the z data like this:
for (n in 1:z)
arr_inv[,,n] <- arr[,,(z-n+1)]
Then try to write like this:
write.table(adply(arr,.margins=c(1,2,3))[4],file=filename,row.names=F,col.names=F)
Nothing I do seems to be fast, so I'm wondering if you know how I should approach this? Thanks