[英]equalizing matrices for combining in abind R
I believe that a similar question was asked here, but I can't seem to find it anymore.我相信这里有人问过类似的问题,但我似乎再也找不到了。
I have two matrices with different dimensions, and I want to equalise them so that I can combine them in an array.我有两个不同维度的矩阵,我想均衡它们,以便我可以将它们组合成一个数组。
for example, I have the following two matrices:例如,我有以下两个矩阵:
a <- matrix(1:6, 3, 2)
b <- matrix(1:12, 4, 3)
a
[,1] [,2]
[1,] 1 4
[2,] 2 5
[3,] 3 6
b
[,1] [,2] [,3]
[1,] 1 5 9
[2,] 2 6 10
[3,] 3 7 11
[4,] 4 8 12
Because I am working with time series data, I would like the added rows/colums to have NAs in them.因为我正在处理时间序列数据,所以我希望添加的行/列中有 NA。 In my example, matrix a would get an extra column and an extra row only containing NAs like this:
在我的示例中,矩阵 a 将获得一个额外的列和一个仅包含 NA 的额外行,如下所示:
[,1] [,2] [,3]
[1,] 1 4 NA
[2,] 2 5 NA
[3,] 3 6 NA
[4,] NA NA NA
In my dataset I will have 79 matrices with unequal dimensions, and I need to make them as big as the matrix with the largest dimensions.在我的数据集中,我将有 79 个维度不等的矩阵,我需要使它们与维度最大的矩阵一样大。
As you only want to extend the small matrix with NA
, we can use a simple approach such as: 由于您只想用
NA
扩展小矩阵,我们可以使用一种简单的方法,例如:
create a matrix as big as b
, with only NA
. 创建一个与
b
一样大的矩阵,只有NA
。 Code: 码:
extended.a = matrix(NA,nrow(b),ncol(b))
fill this matrix with the values from a
. 填补这个矩阵与来自值
a
。 Code: 码:
extended.a[cbind(rep(1:nrow(a),ncol(a)), rep(1:ncol(a),each=nrow(a)))] = a
edit: 编辑:
As per Roland's suggestion, you can also get the vector of indices with which(..., arr.ind=TRUE)
. 根据Roland的建议,你也可以得到索引的向量
which(..., arr.ind=TRUE)
。
For example, which(TRUE | a, arr.ind=TRUE)
例如,
which(TRUE | a, arr.ind=TRUE)
Or even: which(matrix(TRUE,nrow(a),ncol(a), arr.ind=TRUE)
或者甚至:
which(matrix(TRUE,nrow(a),ncol(a), arr.ind=TRUE)
Or far better, using the expand.grid
function: expand.grid(1:nrow(a), 1:ncol(a))
或者更好,使用
expand.grid
函数: expand.grid(1:nrow(a), 1:ncol(a))
If b
is the largest matrix, you can create a matrix with the same dimensions as b
, filled with NA
, and replace the rows and columns corresponding to the smaller matrix a
with the values of a
: 如果
b
是最大的矩阵,你可以创建一个具有相同尺寸的矩阵b
,充满了NA
,并更换相应较小矩阵的行和列a
有值a
:
a2 <- "[<-"(x = matrix(NA, nrow = nrow(b), ncol = ncol(b)),
i = 1:nrow(a), j = 1:ncol(a),
value = a)
a2
# [,1] [,2] [,3]
# [1,] 1 4 NA
# [2,] 2 5 NA
# [3,] 3 6 NA
# [4,] NA NA NA
Example with several matrices, where we find the largest matrix and pad all matrices with NA
to match the dimension of the largest. 有几个矩阵的示例,其中我们找到最大的矩阵并填充所有具有
NA
矩阵以匹配最大的维度。
# create some matrices of different size
a <- matrix(1:6, nrow = 3, ncol = 2)
b <- matrix(1:12, nrow = 4, ncol = 3)
c <- matrix(1:4, nrow = 2, ncol = 2)
# put them in a list
l <- list(a, b, c)
# index of largest (here, max number of rows) matrix in the list
id <- which.max(unlist((lapply(l, nrow))))
# pad matrices with NA
l2 <- lapply(l, function(x){
x <- "[<-"(x = matrix(NA, nrow = nrow(l[[id]]), ncol = ncol(l[[id]])),
i = 1:nrow(x), j = 1:ncol(x),
value = x)
})
l2
# [[1]]
# [,1] [,2] [,3]
# [1,] 1 4 NA
# [2,] 2 5 NA
# [3,] 3 6 NA
# [4,] NA NA NA
#
# [[2]]
# [,1] [,2] [,3]
# [1,] 1 5 9
# [2,] 2 6 10
# [3,] 3 7 11
# [4,] 4 8 12
#
# [[3]]
# [,1] [,2] [,3]
# [1,] 1 3 NA
# [2,] 2 4 NA
# [3,] NA NA NA
# [4,] NA NA NA
I have a very similar problem, but my columns and rows are named and when doing the above solution the names are transformed to v1, v2... How I keep the original names?我有一个非常相似的问题,但我的列和行被命名,并且在执行上述解决方案时,名称将转换为 v1、v2...我如何保留原始名称? below an example using the solution above, I get the "2017_stackoverflow" and I would like to get "2017_aim"
在使用上述解决方案的示例下方,我得到“2017_stackoverflow”,我想得到“2017_aim”
2017 <- matrix(1:6, 3, 2)
2020 <- matrix(1:12, 4, 3)
2017
[,China] [,US]
[Albania,] 0 0
[Macedon,] 1 0
[Portuga,] 0 1
2020
[,China] [,US] [,UK]
[Albania,] 1 0 0
[Macedon,] 1 1 1
[Portuga,] 0 1 1
[Austria,] 0 1 1
2017_stackoverflow
[,V1] [,V2] [,V3]
[V1,] 0 0 10
[V2,] 1 0 10
[V3,] 0 1 10
[V4,] 10 10 10
2017_aim
[,China] [,US] [,UK]
[Albania,] 0 0 10
[Macedon,] 1 0 10
[Portuga,] 0 1 10
[Austria,] 10 10 10
Maybe we can try the following base R option也许我们可以尝试以下基本 R 选项
> replace(NA + b, cbind(c(row(a)), c(col(a))), a)
[,1] [,2] [,3]
[1,] 1 4 NA
[2,] 2 5 NA
[3,] 3 6 NA
[4,] NA NA NA
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.