Christophe Lavaysse
Christophe Lavaysse

Reputation: 115

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

Upvotes: 5

Views: 2255

Answers (1)

DJack
DJack

Reputation: 4940

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

Upvotes: 2

Related Questions