首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在sna包中执行mrqap测试的邻接阵列

在sna包中执行mrqap测试的邻接阵列
EN

Stack Overflow用户
提问于 2016-04-02 04:18:00
回答 1查看 272关注 0票数 1

如何让邻接阵列在sna包中执行mrqap测试?我有一个加权的多网络(两个无向网络和一个有向网络),在具有属性的边缘列表中,如下所示:

代码语言:javascript
复制
source  target  terr    type    weight
  1010    1007     1       3         1
  1011    1303     1       2         1
  1014    1048     1       4         2
  1014    1138     1       4         3

我需要几个具有相同节点数的数组格式的矩阵,类似于这样(但以不同的矩阵格式)​:

代码语言:javascript
复制
type 2
source  target  weight
  1010    1007     0
  1011    1303     1
  1014    1048     0
  1014    1138     0
type 3
source  target  weight
  1010    1007     1
  1011    1303     0
  1014    1048     0
  1014    1138     0
type 4
source  target  weight
  1010    1007     0
  1011    1303     0
  1014    1048     2
  1014    1138     3

我试过的一个脚本是:

代码语言:javascript
复制
el=read.csv("S_EDGES.csv", header = TRUE, sep = ",") # edgelist
Nodos=read.csv("S_NODES.csv", header = TRUE, sep = ",") 
el$type[el$type==2] <- 1 # un solo vínculo de infraestructura

library(igraph)
G=graph.data.frame(el, Nodos, directed=F)

subv = (Nodos$id (Nodos$terr_name=="ART") # this fail and then also "neighverts" and "g3"
SG = decompose.graph(G,mode="weak") # because different territories are in fact different networks
neighverts = unique(unlist(sapply(SG,FUN=function(s){if(any(V(s)$name %in% subv)) V(s)$name else NULL})))
g3 = induced.subgraph(graph=G,vids=neighverts)

# or:
AM=get.adjacency(G, type=c("both"), attr=NULL, names=TRUE, sparse=FALSE) # doesn't distinguish the types of links in different matrices
EN

回答 1

Stack Overflow用户

发布于 2017-01-03 10:28:40

我知道这是一个老问题,但如果您仍然想执行问题中要求的分解:

代码语言:javascript
复制
library(dplyr)

# your example rows
d <- read.table(header = TRUE,
                text = "source  target  terr    type    weight
  1010    1007     1       3         1
  1011    1303     1       2         1
  1014    1048     1       4         2
  1014    1138     1       4         3") %>%
  select(-terr)

# the transformation
lapply(unique(d$type), function(x) {
  mutate(d, weight = ifelse(type == x, weight, 0)) %>%
    select(-type)
}) %>%
  setNames(unique(d$type))

代码将返回您所请求的内容:

代码语言:javascript
复制
$`3`
  source target weight
1   1010   1007      1
2   1011   1303      0
3   1014   1048      0
4   1014   1138      0

$`2`
  source target weight
1   1010   1007      0
2   1011   1303      1
3   1014   1048      0
4   1014   1138      0

$`4`
  source target weight
1   1010   1007      0
2   1011   1303      0
3   1014   1048      2
4   1014   1138      3

从那时起,您应该能够按照自己的意愿转换列表中的每个元素(转换为图形、对边建模等)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36365155

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档