2

I have two high dimensional arrays, both with 1000 rows, 3 columns, 10 slices and 4 groups of slices.

I want to bind them but matching the columns between them. Example below:

#dumb data
array1 <- array(1:4, dim = c(2,1,2,2),dimnames = list(NULL,"Ca",1:2,1:2)); array1
array2 <- array(5:8, dim = c(2,1,2,2),dimnames = list(NULL,"Cb",1:2,1:2)); array2

#desired result 
cbind(array1[,,1,1],array2[,,1,1],array1[,,2,1],array2[,,2,1],
array1[,,1,2],array2[,,1,2],array1[,,2,2],array2[,,2,2])

the outcome is:

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]    1    5    3    7    1    5    3    7
[2,]    2    6    4    8    2    6    4    8

Although column names do not show up, I have ordered manually the columns to be: First column of the first array, first column of the second array, second column of the first array, second column of the second array, and so on.

Is there a other way to do this than manually using cbind?

Thanks in advance!

Oscar L
  • 35
  • 6

1 Answers1

0

Here is one way to accomplish this for the example, but might require some modification for the actual data set:

m1 <- cbind(array1, array2)
l1 <- lapply(seq_len(nrow(m1) / 2), function(x) m1[seq(2 * x - 1, 2 * x), ])
m2 <- do.call(cbind, l1)

> m2
     array1 array2 array1 array2 array1 array2 array1 array2
[1,]      1      5      3      7      1      5      3      7
[2,]      2      6      4      8      2      6      4      8
manotheshark
  • 4,297
  • 17
  • 30