我有一个大的对称矩阵,里面装有权重:
AT BE CH CZ
AT 0 0.00276 0.148 0.109
BE 0.00276 0 0.145 0.112
CH 0.148 0.145 0 0.257
CZ 0.109 0.112 0.257 0
我需要创建一个数据框,列出所有元素之间的链接(除了相同的,例如AT和AT,BE和BE等)和相应的权重。换句话说,我不知道如何使用矩阵中的数据填充数据框。数据框看起来应该与
类似df<-data.frame(from = c("AT", "BE", "CH", "CZ"), to= c("BE", "CH", "CZ", "AT"),
weight=c(0.003,0.145,0.257,0.109))
我需要这种类型的数据框,以 igraph 进一步可视化,这里建议Visualizing data on geographic map with networks (R)
答案 0 :(得分:1)
矩阵持有权重:
mtx <- matrix(
c(0,0.00276,0.148,0.109,0.00276,0,0.145,0.112,0.148,0.145,0,0.257,0.109,0.112,0.257,0),
nrow=4,
ncol=4)
rownames(mtx) <- c('AT','BE','CH','CZ')
colnames(mtx) <- c('AT','BE','CH','CZ')
功能用于将权重矩阵转换为权重框架:
mtx_to_igraph_frame <- function(mtx) {
combs <- expand.grid(rownames(mtx), colnames(mtx))
combs <- subset(combs, Var1 != Var2)
combs <- t(apply(combs, 1, sort))
combs <- combs[!duplicated(combs),]
extract_vals <- NULL
for(i in 1:nrow(combs)) { extract_vals[i] <- mtx[combs[i,1],combs[i,2]] }
combs <- data.frame(combs)
combs$weight <- extract_vals
names(combs) <- c('from', 'to', 'weight')
row.names(combs) <- NULL
return(combs)
}
<强>用法强>:
mtx_to_igraph_frame(mtx)
<强>结果强>:
答案 1 :(得分:0)
如果我们将矩阵转换为数据框,我相信我们可以做类似的事情:
library(dplyr)
library(tidyr)
df %>%
gather(from, weight_index) %>%
group_by(from) %>%
mutate(weight = lead(weight_index, default = weight_index[1])) %>%
filter(weight_index == 0) %>%
ungroup() %>%
mutate(to = lead(from, default = from[1])) %>%
select(from, to, weight)
# A tibble: 4 x 3 from to weight <chr> <chr> <dbl> 1 AT BE 0.00276 2 BE CH 0.145 3 CH CZ 0.257 4 CZ AT 0.109
gather
格式化为长格式。这会创建变量from
和weight_index
。from
分组(即AT
,BE
,CH
,CZ
)。weight
的{{1}} var并回收值,将weight_index
值(由组NA
生成)替换为lead
的第一个weight_index
值每个小组。filter
行weight_index == 0
。ungroup
to
的{{1}} var并循环使用值,将from
值替换为第一个NA
值(即给出var {{1}的第4行} value from
)。to
我们想要的列以及所需的顺序。数据:
AT
答案 2 :(得分:0)
也许以下会做你想要的。请注意最后weight
的值的差异。
首先,输入数据。
mat <-
structure(c(0, 0.00276, 0.148, 0.109, 0.00276, 0, 0.145, 0.112,
0.148, 0.145, 0, 0.257, 0.109, 0.112, 0.257, 0), .Dim = c(4L,
4L), .Dimnames = list(c("AT", "BE", "CH", "CZ"), c("AT", "BE",
"CH", "CZ")))
现在,代码。
mat2 <- cbind(mat[, -1], mat[, 1])
colnames(mat2)[ncol(mat2)] <- colnames(mat)[1]
mat2
df2 <- data.frame(from = rownames(mat2), to = colnames(mat2), weight = diag(mat2))
df<-data.frame(from = c("AT", "BE", "CH", "CZ"), to= c("BE", "CH", "CZ", "AT"),
weight=c(0.003,0.145,0.257,0.109))
all.equal(df, df2)
#[1] "Component “weight”: Mean relative difference: 0.08"
此“错误”是由于舍入错误引起的,例如,您在示例输出中将0.00276
转换为0.003
。