R-如何使2个邻接矩阵彼此兼容 [英] R - How to make 2 adjacency matrices compatible to eachother

查看:96
本文介绍了R-如何使2个邻接矩阵彼此兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个具有不同尺寸的邻接矩阵.我想使它们的尺寸兼容,以便当我从第二个矩阵的任何列替换一个矩阵的任何列时,我不会收到以下错误消息: 错误:要替换的项目数不是替换长度的倍数 这是我的矩阵:

I have 2 adjacency matrices with different dimesnsions. I want to make their dimensions compatible so that when I replace any column of one matrix from any column of second matrix then I don't get the following error message: Error: number of items to replace is not a multiple of replacement length Here are my matrices:

> mat1
      Tommy Roy Addy Sam
Tommy     0   1    0  -1
Roy      -1  -1    1   0
Addy      1   0   -1   0
Sam       0   0   -1   1

> mat2
     Mike Roy Addy Sam Dan
Mike    0   1    0  -1   0
Roy    -1  -1    1   0   1
Addy    1   0   -1   0  -1
Sam     0   0   -1   1   0
Dan     1   0    0  -1   1

要使mat1与mat2兼容,我必须在mat1中添加2列和2行,使其变为:

To make the mat1 compatible to mat2 I have to add 2 columns and 2 rows in mat1, so that it become:

> newMat1
      Tommy Roy Addy Sam Mike Dan
Tommy     0   1    0  -1    0   0
Roy      -1  -1    1   0    0   0
Addy      1   0   -1   0    0   0
Sam       0   0   -1   1    0   0
Mike      0   0    0   0    0   0
Dan       0   0    0   0    0   0

此处添加了2个新的列和行(MikeDan),因为它们以前不存在,但属于第二个矩阵.请注意,新添加的行和列已使用0值初始化.同样,newMat2将变为:

Here 2 new column and rows have been added (Mike and Dan) as they were not there before but were part of the second matrix. Please notice that newly added rows and columns have been initialized with 0 value. Similarly newMat2 will become:

> newMat2
      Mike Roy Addy Sam Dan Tommy
Mike     0   1    0  -1   0     0
Roy     -1  -1    1   0   1     0
Addy     1   0   -1   0  -1     0
Sam      0   0   -1   1   0     0
Dan      1   0    0  -1   1     0
Tommy    0   0    0   0   0     0

这是原始矩阵输出:

> dput(mat1)
structure(c(0L, -1L, 1L, 0L, 1L, -1L, 0L, 0L, 0L, 1L, -1L, -1L, 
-1L, 0L, 0L, 1L), .Dim = c(4L, 4L), .Dimnames = list(c("Tommy", 
"Roy", "Addy", "Sam"), c("Tommy", "Roy", "Addy", "Sam")))
> dput(mat2)
structure(c(0L, -1L, 1L, 0L, 1L, 1L, -1L, 0L, 0L, 0L, 0L, 1L, 
-1L, -1L, 0L, -1L, 0L, 0L, 1L, -1L, 0L, 1L, -1L, 0L, 1L), .Dim = c(5L, 
5L), .Dimnames = list(c("Mike", "Roy", "Addy", "Sam", "Dan"), 
    c("Mike", "Roy", "Addy", "Sam", "Dan")))

如上所述,我想稍后替换矩阵之间的列,问题是当我这样做时,colnames和rowname的不同顺序会影响索引中的值.例如:

As mentioned in question, I want to later replace the the columns between the matrices and problem is that when I do this the different ordering of colnames and rownames affect the values in the indices. for example:

Change <- c("Mike", "Dan")
for(i in 1:length(Change)){
 ifelse(Change[i] %in% colnames(newMat1), newMat1[,Change[i]] <- newMat2[,Change[i]], newMat1[,Change[i]][newMat1[,Change[i]] == 1] <- 0)}
newMat1
      Tommy Roy Addy Sam Mike Dan
Tommy     0   1    0  -1    0   0
Roy      -1  -1    1   0   -1   1
Addy      1   0   -1   0    1  -1
Sam       0   0   -1   1    0   0
Mike      0   0    0   0    1   1
Dan       0   0    0   0    0   0

在此,newMat1中的Mike列已由newMat2中的Mike列替换.如您所见,在原始newMat2中,索引Mike-to-Mike的索引为0,而在新获取的newMat1中的索引为1,这是因为行名和列名的顺序不同.

Here the Mike column in newMat1 has been replaced by Mike column in newMat2. As you can see that index Mike-to-Mike is 0 in original newMat2 but it is 1 in freshly obtained newMat1, and that is because of ordering of rownames and colnames is different.

答案:为此,需要订购,并且通过以下方式完成:

Answer: For this purpose ordering was required and it was done by:

newMat2 <- newMat2[rownames(newMat1), colnames(newMat1)]

推荐答案

一个简单的功能:

complete_matrix <- function(mat, ref) {
  dif <- setdiff(rownames(ref), rownames(mat))
  mat <- rbind(mat, matrix(0, length(dif), ncol(mat), dimnames = list(dif, NULL)))
  mat <- cbind(mat, matrix(0, nrow(mat), length(dif), dimnames = list(NULL, dif)))
  return(mat)
}

newMat1 <- complete_matrix(mat1, mat2)
newMat2 <- complete_matrix(mat2, mat1)

它首先在焦点矩阵mat和参考矩阵ref之间找到丢失的名称,然后将两个带有0的矩阵绑定为丢失的名称.

It first finds the missing names between the focal matrix mat and the reference matrix ref, then binds two matrices with 0s for the missing names..

> newMat1 
      Tommy Roy Addy Sam Mike Dan
Tommy     0   1    0  -1    0   0
Roy      -1  -1    1   0    0   0
Addy      1   0   -1   0    0   0
Sam       0   0   -1   1    0   0
Mike      0   0    0   0    0   0
Dan       0   0    0   0    0   0
> newMat2 
      Mike Roy Addy Sam Dan Tommy
Mike     0   1    0  -1   0     0
Roy     -1  -1    1   0   1     0
Addy     1   0   -1   0  -1     0
Sam      0   0   -1   1   0     0
Dan      1   0    0  -1   1     0
Tommy    0   0    0   0   0     0

另一种解决方案:

complete_matrix2 <- function(mat, ref) {
  nam <- union(rownames(ref), rownames(mat))
  out <- matrix(0, length(nam), length(nam), dimnames = list(nam, nam))
  out[rownames(mat), colnames(mat)] <- mat
  return(mat)
}

这篇关于R-如何使2个邻接矩阵彼此兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆