1

How can I get an adjacency array to perform an mrqap test in sna package? I have a weighted multiple network (two undirected and one directed), in an edge list with attributes, like this:

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

I need several matrix with the same number of nodes in array format, something like this (but in adyacency matrix format)​:

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

One script i've tried:

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
Sebastián
  • 111
  • 8
  • Thanks @user20650 for your suggestion, I improved the question. I need to differentiate the types of link in each matrix (it was a multigraph) maintaining the same structure (same number of nodes, whether or not related to each type) – Sebastián Apr 03 '16 at 20:59
  • Hi, @Sebastian, thanks for updating. I dont know what you are trying to do, sorry. But I think for someone to help, you need to try an make your example reproducible. So define a few rows of `el` and of `Nodes` in your question as an example, and for that example show what the output adjacency matrix would look like.Thanks – user20650 Apr 04 '16 at 00:01
  • Thanks again @user20650. I give an example of "el" in the first table (source target terr type weight), the base Nodes isn't important now – Sebastián Apr 04 '16 at 02:19

1 Answers1

0

I understand this is an old question, but in case you still want to perform the decomposition requested in your question:

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))

The code returns what you asked for:

$`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

From there on, you should be able to transform each element of the list as you wish (convert to graph, model the edges, etc.).

Fr.
  • 2,865
  • 2
  • 24
  • 44
  • Thanks for your suggestions. I solved it in another way, turning each type into a different edge list. – Sebastián Jan 04 '17 at 17:18
  • Fair enough. You might also want to take a look at `purrr`, which deals well with these kinds of data issues. I don't use it myself yet, but am planning to do so very soon. See [these slides](https://speakerdeck.com/jennybc/data-rectangling) for an introduction. – Fr. Jan 04 '17 at 23:40
  • Thank you very much. Now I'm with my thesis but I'll see later. – Sebastián Jan 05 '17 at 21:08