2013-03-05 57 views
22

如果我有幾個我創建的矩陣,我怎樣才能將它們組合成一個數組?我有8個矩陣,每個矩陣有200行和200列,我需要將它們組合成一個dim = 200,200,8的數組。所以我希望每個矩陣都是我的數組的一部分。將矩陣組合到一個數組中R

+0

不結果必須是'數組'? 'list(x,y)'會爲你工作嗎? – thelatemail 2013-03-05 00:22:12

回答

14

這裏是兩個例子。你可以很容易地擴展這個八

# create two matricies with however many rows and columns 
x <- matrix(1:9 , 3 , 3) 
y <- matrix(10:18 , 3 , 3) 
# look at your starting data 
x 
y 

# store both inside an array, with the same first two dimensions, 
# but now with a third dimension equal to the number of matricies 
# that you are combining 
z <- array(c(x , y) , dim = c(3 , 3 , 2)) 

# result 
z 
0

如何:

combmat <- array(dim=c(200,200,8), data=cbind(matrix1,matrix2,...,matrix8)) 
21

可以使用abind函數從abind包:

library(abind) 
newarray <- abind(mat1, mat2, mat3, mat4, along=3) 

## or if mats are in a list (a good idea) 

newarray <- abind(matlist, along=3) 
+3

絕對要走的路,因爲'abind'接管了用戶的繁瑣且容易出錯的索引簿記。 – 2013-03-05 00:27:32

2

這取決於你是否要結合他們列主要或行主要。這類似於使用cbindrbind將矢量組合到矩陣中。因爲ř存儲在列主順序矩陣,這是最容易實現:

matrices <- list(
    matrix(1:9 , 3 , 3), 
    matrix(10:18 , 3 , 3) 
); 

#it is assumed all matrices in the list have equal dimensions 
array1 <- array(
    data = do.call(cbind, matrices), 
    dim = c(dim(matrices[[1]]), length(matrices)) 
); 

新的層面(2在這種情況下)將成爲第三維度。從打印方法的輸出來看,這看起來正確的,因爲它的最後一維分割打印:

> print(array1) 
, , 1 

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

, , 2 

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

不過,有時你可能需要將其由第一個維度,而不是相結合,如:

array2 <- array (
    data = do.call(rbind, lapply(matrices, as.vector)), 
    dim = c(length(matrices), dim(matrices[[1]])) 
); 

print(array2[1,,]) 

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

例如,假設您想將這些矩陣分配給帶有列的數據框;每行一個矩陣。然後,第一尺寸,a.k.a nrow具有在陣列和數據幀以匹配:

mydf <- data.frame(foo = 1:2, row.names=c("first", "second")) 
mydf$bar <- array1 
    Error in `$<-.data.frame`(`*tmp*`, "bar", value = 1:18) : 
    replacement has 3 rows, data has 2 

mydf$bar <- array2 
mydf$bar 
6

下面是類似於abind -ing一個版本,但不使用任何額外的軟件包。收集一切都變成list,然後用sapply的選項simplify="array",無爲列表中的每個部分後(identity剛剛返回的對象,等同於function(x) x):

sapply(list(x,y), identity, simplify="array") 
# similarly to save a couple of keystrokes, the following is usually identical 
sapply(list(x,y), I, simplify="array") 

#, , 1 
# 
#  [,1] [,2] [,3] 
#[1,] 1 4 7 
#[2,] 2 5 8 
#[3,] 3 6 9 
# 
#, , 2 
# 
#  [,1] [,2] [,3] 
#[1,] 10 13 16 
#[2,] 11 14 17 
#[3,] 12 15 18 

如果你想保留每個原始矩陣的名字在新的陣列作爲標識符,嘗試:

sapply(mget(c("x","y")), identity, simplify="array") 
+0

函數aperm可以進一步用於改變三維數組的排列順序:new.mat < - aperm(old.mat,c(3,1,2))。這樣new.mat [1,]將對應於矩陣[[1]]。 – 2017-09-30 20:50:33

0

相關:How to stack multiple matrices in R

到目前爲止所有解決方案的問題是,當矩陣(不是data.frame s - 對於此dplyrdata.table工作正常)不具有相同的行和列順序時,綁定將堆疊彼此不相關的值。

如果您要檢查,並考慮到在每個維度的名字,看看narray

enter image description here

(免責聲明:我寫的包)

0
library('abind') 
abind(m1, m2, m3, along = 2.5) 
abind(m1, m2, m3, along = 3) 

m4 <- list(m1, m2, m3) 
abind(m4, along = 3) 

     along  input = matrix + matrix      output 
---------------------------------------------------------------------------- 
     0   split columns and row bind them     array 
     0.5  same as 0          array 
     1   combine matrices into one matrix by rowwise  matrix 
     1.5  split columns and column bind them    array 
     2   combine matrices into one matrix by columnwise matrix 
     2.5  Form an array with matrices      array 
     3   Same as 2.5          array