vbltx
vbltx

Reputation: 11

array_reshape() not reshaping arrays as desired

I have "old_array", I want to reshape it to become "new_array" using array_reshape()

old_array <- array(seq(1,30,1),c(2,3,5))
new_array <- t(array(seq(1,30,1),c(6,5)))

The old_array is:

, , 1

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

, , 2

     [,1] [,2] [,3]
[1,]    7    9   11
[2,]    8   10   12

, , 3

     [,1] [,2] [,3]
[1,]   13   15   17
[2,]   14   16   18

, , 4

     [,1] [,2] [,3]
[1,]   19   21   23
[2,]   20   22   24

, , 5

     [,1] [,2] [,3]
[1,]   25   27   29
[2,]   26   28   30

The new_array is:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
[5,]   25   26   27   28   29   30

I tried the following code, however the reshaped array is not the way I want:

array_reshape(old_array,c(6,5))

Expected results:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
[5,]   25   26   27   28   29   30

Actual results:

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    7   13   19   25
[2,]    3    9   15   21   27
[3,]    5   11   17   23   29
[4,]    2    8   14   20   26
[5,]    4   10   16   22   28
[6,]    6   12   18   24   30

Upvotes: 1

Views: 546

Answers (2)

AC0519
AC0519

Reputation: 76

First, you want a 5x6 matrix. Your current code is asking for a return of 6x5 so you would want to write

array_reshape(old_array, c(5,6))

However, your old_array is returning 5 different matrices all 2x3. Since you are filling it in by row, it looks like array_reshape will take the first value from each row of each separate matrix and then since you are telling it to have 6 columns it then grabs the second value of the first matrix to fill out the first row of the 5x6. It then repeats this pattern to fill in the other 4 rows. This will return:

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    7   13   19   25    3
[2,]    9   15   21   27    5   11
[3,]   17   23   29    2    8   14
[4,]   20   26    4   10   16   22
[5,]   28    6   12   18   24   30   

Can you remove c(2,3,5) from your old_array line? It will work fine then. Otherwise array_reshape is not the appropriate function in this case. But, if you really want to use it, you can tell it to fill a 6x5 matrix by column and then transpose the matrix. This will give you the result you want:

t(array_reshape(old_array, c(6,5), "F"))

     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    1    2    3    4    5    6
[2,]    7    8    9   10   11   12
[3,]   13   14   15   16   17   18
[4,]   19   20   21   22   23   24
[5,]   25   26   27   28   29   30

Upvotes: 1

Chase
Chase

Reputation: 69151

You can call matrix and specify the dimensions you desire. Given how R fills matrices, you need to specify byrow = TRUE in this scenario:

old_array <- array(seq(1,30,1),c(2,3,5))
matrix(old_array, nrow = dim(old_array)[3], ncol = prod(dim(old_array)[1:2]), byrow = TRUE)
#>      [,1] [,2] [,3] [,4] [,5] [,6]
#> [1,]    1    2    3    4    5    6
#> [2,]    7    8    9   10   11   12
#> [3,]   13   14   15   16   17   18
#> [4,]   19   20   21   22   23   24
#> [5,]   25   26   27   28   29   30

Created on 2019-03-31 by the reprex package (v0.2.1)

Upvotes: 1

Related Questions