[英]Combining matrices into an array in R
如果我創建了多個矩陣,如何將它們組合成一個數組? 我有 8 個矩陣,每個矩陣有 200 行和 200 列,我需要將它們組合成一個數組,dim = 200,200,8。 所以我希望我的每個矩陣都是我數組的一部分。
您可以使用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 )
這是兩個的例子。 您可以輕松地將其擴展到八個
# 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
簡短的版本是您可以使用以下函數將一組矩陣簡化為數組: simplify2array
:
simplify2array(list(x,y))
下面是我之前的答案,顯示了如何使用sapply
的simplify=
參數執行此操作,因為當 'simplify' 不為 false 時, 'simplify2array()' 是從 'simplify()' 調用的實用程序- 請參閱?sapply
幫助文件。
這是一個類似於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")
這取決於您是要以列為主還是以行為主將它們組合在一起。 這類似於使用cbind
和rbind
將向量組合成矩陣。 因為 R 以列優先順序存儲矩陣,所以這是最容易實現的:
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)將成為第 3 個維度。 從print方法的輸出來看,這看起來很准確,因為它是按照最后一個維度來分割print的:
> 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
例如,假設您要將這些矩陣分配給具有列的數據框; 每行一個矩陣。 然后第一個維度,也就是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
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
這個怎么樣:
combmat <- array(dim=c(200,200,8), data=cbind(matrix1,matrix2,...,matrix8) )
相關: 如何在 R 中堆疊多個矩陣
到目前為止所有解決方案的問題是,當矩陣(不是data.frame
s - 對於這個dplyr
和data.table
工作正常)沒有相同的行和列順序時,bind 會將不相關的值堆疊在一起.
如果要檢查並考慮每個維度中的名稱,請查看narray
:
(免責聲明:我寫了包)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.