Search code examples
arraysrmergeapplydimensions

How to reduce the dimensions of an array in R


I have an array with 4 dimensions c(12000, 100, 20, 4) and I would like to merge two dimensions in one (the second and the third) to create a new array with 3 dimensions c(12000, 2000, 4).

When using 2 dimensions array, the function I use is as.vector, but I'm stuck for bigger arrays. Is there a function like apply?

Thank you


Solution

  • Some sample data:

    x <- array(1:(12000*100*20*4), dim=c(12000, 100, 20, 4))
    dim(x)
    
    [1] 12000   100    20     4
    

    Use wrap from R.utils to merge all dimensions (NA) except for 1 and 4:

    library(R.utils)
    y <- wrap(x, map=list(1, NA, 4))
    dim(y)
    
    [1] 12000   2000    4