如果我有幾個我創建的矩陣,我怎樣才能將它們組合成一個數組?我有8個矩陣,每個矩陣有200行和200列,我需要將它們組合成一個dim = 200,200,8的數組。所以我希望每個矩陣都是我的數組的一部分。將矩陣組合到一個數組中R
回答
這裏是兩個例子。你可以很容易地擴展這個八
# 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
如何:
combmat <- array(dim=c(200,200,8), data=cbind(matrix1,matrix2,...,matrix8))
可以使用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)
絕對要走的路,因爲'abind'接管了用戶的繁瑣且容易出錯的索引簿記。 – 2013-03-05 00:27:32
這取決於你是否要結合他們列主要或行主要。這類似於使用cbind
和rbind
將矢量組合到矩陣中。因爲ř存儲在列主順序矩陣,這是最容易實現:
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
下面是類似於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")
函數aperm可以進一步用於改變三維數組的排列順序:new.mat < - aperm(old.mat,c(3,1,2))。這樣new.mat [1,]將對應於矩陣[[1]]。 – 2017-09-30 20:50:33
相關:How to stack multiple matrices in R
到目前爲止所有解決方案的問題是,當矩陣(不是data.frame
s - 對於此dplyr
和data.table
工作正常)不具有相同的行和列順序時,綁定將堆疊彼此不相關的值。
如果您要檢查,並考慮到在每個維度的名字,看看narray
:
(免責聲明:我寫的包)
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
- 1. 將多個數據矩陣組合到R列表中
- 2. 將多個矩陣導入到R中的一個數組中
- 3. 將一個三維數組轉換成一個矩陣R
- 4. 將數組轉換爲矩陣R
- 5. 合併多個組合一起矩陣
- 6. 矩陣推到一個數組
- 7. MATLAB - 矩陣組合
- 8. 在numpy中組合矩陣
- 9. R:從列表中創建一個矩陣數組
- 10. R:使用索引矩陣從數組中提取矩陣
- 11. 在R中組合3個不同的矩陣圖
- 12. R - 在矩陣佈局中組合10個圖塊
- 13. 如何將多個數組組合到一個新數組中?
- 14. 如何從一組數據中創建一個數組/矩陣
- 15. 如何將2個矩陣組合成一個圖
- 16. Tensorflow:我如何將3D矩陣組合爲一個整體矩陣?
- 17. 矩陣矩陣在Matlab中的數組?
- 18. 如何將對象數組值組合到一個數組中?
- 19. R:將列表列表組合到一個數據框中
- 20. 組合布爾矩陣
- 21. 矩陣元素組合
- 22. R:如何兩個相關組合矩陣使用GGPLOT2
- 23. 在一組矩陣
- 24. 將陣列數組轉換爲矩陣
- 25. 將矩陣分成子組矩陣
- 26. 將矩陣組中的元素組合爲4而不重複
- 27. 組合兩個numpy的矩陣
- 28. 將二維數組的一行分配到一維矩陣
- 29. 如何將數組數組的數組合併到一個數組中?
- 30. 如何將多個矩陣幀合併爲一個使用R?
不結果必須是'數組'? 'list(x,y)'會爲你工作嗎? – thelatemail 2013-03-05 00:22:12