之後我需要創建一個按行,從左到右和後代排序的3D數組。由R中的行填充的3D數組從左到右排序,在後裔
x <- 100
我已經試過這一點:
b <- array(1:96, dim= c(8,4,3))
但它首先排序descendently。使用apperm(b)不工作,以及
我想結果是這樣的:
, , 1
1 2 3 4 5
6 7 8 9 10
11 12 13 14
15 16 17 18
19 20 21 22
之後我需要創建一個按行,從左到右和後代排序的3D數組。由R中的行填充的3D數組從左到右排序,在後裔
x <- 100
我已經試過這一點:
b <- array(1:96, dim= c(8,4,3))
但它首先排序descendently。使用apperm(b)不工作,以及
我想結果是這樣的:
, , 1
1 2 3 4 5
6 7 8 9 10
11 12 13 14
15 16 17 18
19 20 21 22
array
默認填充值沿着第一維度,那麼第二尺寸,然後第三;你所尋找的是填補它的(第2,第1,第3)的順序,你可以用第一維和第二維形狀數組初始化切換,然後在其上使用aperm
:
b <- aperm(array(1:96, dim= c(4,8,3)), c(2,1,3))
# ^^ ^^ switch the dimension twice here
b
, , 1
[,1] [,2] [,3] [,4]
[1,] 1 2 3 4
[2,] 5 6 7 8
[3,] 9 10 11 12
編輯:我第一次嘗試,但@ Psidom的回答是這樣做的正確方法。
您需要將它作爲3個矩陣的組合,然後將它們組合成一個數組。在下面的代碼中,我使用了96*i/3
來使它對3個以上的矩陣進行靈活組合。
b <- array(c(aperm(array(1:(96*1/3), dim = c(4,8))) ,
aperm(array(33:(96*2/3), dim = c(4,8))),
aperm(array(65:(96*3/3), dim = c(4,8)))) ,
dim = c(8 , 4 , 3))
這將是輸出:
b[, , 1]
# [,1] [,2] [,3] [,4]
# [1,] 1 2 3 4
# [2,] 5 6 7 8
# [3,] 9 10 11 12
# [4,] 13 14 15 16
# [5,] 17 18 19 20
# [6,] 21 22 23 24
# [7,] 25 26 27 28
# [8,] 29 30 31 32
https://stackoverflow.com/questions/31755233/r-fill-multidimensional-array-by-row – Masoud
他們使用apperm,但在這種情況下不起作用 –