从非方矩阵创建邻接矩阵
Creating Adjacency Matrix from non square MATRIX
我想从非方阵创建邻接矩阵。行和列都定义了不同的变量。在示例中,行代表独特的基金,其中列代表独特的公司。
此代码创建具有重复值的 10x10 矩阵,我想创建 6x6 矩阵,如下所示
expand.matrix <- function(A){
m <- nrow(A)
n <- ncol(A)
B <- matrix(0,nrow = m, ncol = m)
C <- matrix(0,nrow = n, ncol = n)
cbind(rbind(B,t(A)),rbind(A,C))
}
df <- expand.matrix(dat)
我的矩阵是
dat <- structure(c(0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1,
1), .Dim = c(3L, 6L), .Dimnames = structure(list(c("Fund 1",
"Fund2", "Fund3"), c("Firm A", "Firm B", "Firm C", "Firm D",
"Firm E", "Firm F")), .Names = c("", "")), class = "table")
预期结果
我试图将 data-set 的原始 class 保留为 table。
#Code
dat <- t(dat)
dat <- cbind(dat,0,0,0)
colnames(dat) <- c('Fund1','Fund2','Fund3','4','5','6')
我想从非方阵创建邻接矩阵。行和列都定义了不同的变量。在示例中,行代表独特的基金,其中列代表独特的公司。
此代码创建具有重复值的 10x10 矩阵,我想创建 6x6 矩阵,如下所示
expand.matrix <- function(A){
m <- nrow(A)
n <- ncol(A)
B <- matrix(0,nrow = m, ncol = m)
C <- matrix(0,nrow = n, ncol = n)
cbind(rbind(B,t(A)),rbind(A,C))
}
df <- expand.matrix(dat)
我的矩阵是
dat <- structure(c(0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 1, 1,
1), .Dim = c(3L, 6L), .Dimnames = structure(list(c("Fund 1",
"Fund2", "Fund3"), c("Firm A", "Firm B", "Firm C", "Firm D",
"Firm E", "Firm F")), .Names = c("", "")), class = "table")
预期结果
我试图将 data-set 的原始 class 保留为 table。
#Code
dat <- t(dat)
dat <- cbind(dat,0,0,0)
colnames(dat) <- c('Fund1','Fund2','Fund3','4','5','6')