0

I am working with network adjacency matrices in R for a QAP regression. Currently, the matrices look like this (ignore 0s and 1s):enter image description here

How can I make the column/row labels appear in the same order between time 1 and 2? I have sorted the initial edgelists (q1 and q2 in the code below), but something appears to get lost in translation when converting these to one mode adjacency matrices.

q1_2001<-graph.data.frame(q1, directed=FALSE)
q2_2002<-graph.data.frame(q2, directed=FALSE)
V(q1_2001)$type <- bipartite_mapping(q1_2001)$type
V(q2_2002)$type <- bipartite_mapping(q2_2002)$type
q1bp_2001<-bipartite.projection(q1_2001)
q2bp_2002<-bipartite.projection(q2_2002)
q1edge2001<-as_edgelist(q1bp_2001$proj1)
q1edge2002<-as_edgelist(q2bp_2002$proj1)
##Predictor Matrix (IV)
qap2001A<-get.adjacency(graph.data.frame(q1edge2001),sparse = FALSE)
qap2001A[order(decreasing = TRUE)]
##Response Matrix (DV)
qap2002B<-get.adjacency(graph.data.frame(q1edge2002),sparse = FALSE)
Holly L
  • 23
  • 1
  • 4

1 Answers1

0

Assuming that the 2 matrices are of the dimensions and the column names and row names are the same (although they may appear in different order), you can get the desired result with the following:

Time2 = Time2[rownames(Time1), colnames(Time1)]

For example

set.seed(1)
Time1 = replicate(5, sample(0:1, 5, replace = T)) 
Time2 = replicate(5, sample(0:1, 5, replace = T)) 

colnames(Time1) = rownames(Time1) = c("22B", "13B", "999B", "11B", "03B")
colnames(Time2) = rownames(Time2) = c("22B", "999B", "11B", "13B", "03B")

> Time1
     23B 13B 999B 11B 03B
23B    0   1    0   0   1
13B    0   1    0   1   0
999B   1   1    1   1   1
11B    1   1    0   0   0
03B    0   0    1   1   0

> Time2
     22B 999B 11B 13B 03B
22B    0    0   1   1   1
999B   0    1   1   1   0
11B    0    0   0   1   0
13B    1    0   1   1   1
03B    0    1   0   1   1

# Notice that Time1 and Time2 are similar to your example but with different entries. The column names and row names are the same but in different order

Time2 = Time2[rownames(Time1), colnames(Time1)]

> Time2
     22B 13B 999B 11B 03B
22B    0   1    0   1   1
13B    1   1    0   1   1
999B   0   1    1   1   0
11B    0   1    0   0   0
03B    0   1    1   0   1
NM_
  • 1,887
  • 3
  • 12
  • 27
  • Thank you NM_! I tried this on my datasets but it does not work. qap2002B<-qap2002B[rownames(qap2001A,colnames(qap2001A))] it returns a values list and no adjacency matrix. – Holly L Mar 22 '19 at 13:44
  • @HollyL, it might be an issue with the order of your parenthesis. Please try `qap2002B = qap2002B[ rownames(qap2001A), colnames(qap2001A) ]` – NM_ Mar 22 '19 at 13:47
  • @HollyL, could you provide `q1` and `q2` so that your code is reproducible please? This might help with solving the issue. – NM_ Mar 22 '19 at 16:57