2

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

4

1 回答 1

1

一种(复杂的)方法是将数组在第三维上拆分为列表元素,lapply然后使用它mapply来重塑矩阵并最终将其转换为输出向量:

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

另外,下次请包括一个可重复的示例

于 2013-08-01T11:04:45.600 回答