Reputation: 606
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
Upvotes: 2
Views: 88
Reputation: 59980
One (convoluted) way would be to split the array across the third dimension into list elements using lapply
then use mapply
to reshape the matrix and finally turn it into an output vector:
mat <- array( rep(1:9,each=3) , dim = c(3,3,3) )
mat
, , 1
[,1] [,2] [,3]
[1,] 1 2 3
[2,] 1 2 3
[3,] 1 2 3
, , 2
[,1] [,2] [,3]
[1,] 4 5 6
[2,] 4 5 6
[3,] 4 5 6
, , 3
[,1] [,2] [,3]
[1,] 7 8 9
[2,] 7 8 9
[3,] 7 8 9
as.vector( t( mapply( c , lapply( 1:3 , function(x) as.vector( t(mat[,,x]) ) ) ) ) )
[1] 1 4 7 2 5 8 3 6 9 1 4 7 2 5 8 3 6 9 1 4 7 2 5 8 3 6 9
As anside, please include a reproducible example next time
Upvotes: 1