3

I'm having a matrix like the following one

m <- expand.grid(LETTERS[1:24],LETTERS[1:24])
m$weight <- runif(nrow(m), 0.01, max = 1)
m <- m[m$Var1!=m$Var2, ] ##remove loop edges
colnames(m) = c("to","from","weight")

and in this form it describes a directed graph. What I want to do is to subtract and take the absolute value of each pair of inverse edges and create a new matrix describing a new undirected graph. i.e:

abs( edge_weight(A,B) - edge_weight(B,A) )

But I don't know how to take into account only once each pair.

zx8754
  • 52,746
  • 12
  • 114
  • 209
J. Doe
  • 619
  • 4
  • 16

3 Answers3

2

Using igraph

library(igraph)

#dataframe to directed graph
directed_graph <- graph.data.frame(m, directed = T)

#convert to undirected graph by applying desired function
undirected_graph <- as.undirected(directed_graph, mode = "collapse", 
                                  edge.attr.comb = list(weight = function(x) abs(x[1] - x[2])))

#final result
df <- as.data.frame(cbind(get.edgelist(undirected_graph), 
                          unlist(get.edge.attribute(undirected_graph))))
colnames(df) <- c("edge1", "edge2", "weight")
rownames(df) <- NULL

which gives

> head(df)
  edge1 edge2             weight
1     B     C  0.310624073725194
2     B     D  0.587582074650563
3     C     D 0.0327853348944336
4     B     E   0.19360910307616
5     C     E  0.328824346032925
6     D     E   0.13037203295622


Sample data:

set.seed(123)

m <- expand.grid(LETTERS[1:24], LETTERS[1:24])
m$weight <- runif(nrow(m), 0.01, max = 1)
m <- m[m$Var1 != m$Var2, ]
colnames(m) <- c("to", "from", "weight")
Prem
  • 11,775
  • 1
  • 19
  • 33
1

Convert the directed arcs to common edge pairs to identify the inverse pair of arcs.

Step1: Sorted arcs to find edge/ inverse arc pairs:

edge <- as.data.frame( t( apply(m[c("to","from")], 1, sort)))
names(edge) <- c("edge_to" , "edge_from")

Step2: Combined and summarized to get the absolute difference in weights.

new_m <- cbind(m, edge)

library(dplyr)
new_m %>% 
     group_by(edge_to, edge_from) %>% 
         summarise(new_weight = abs(weight[1] - weight[2]))

#   edge_to edge_from new_weight
#   <fct>   <fct>          <dbl>
# 1 A       B            0.0477 
# 2 A       C            0.0133 
# 3 A       D            0.162  
# 4 A       E            0.690  
# 5 A       F            0.00987
# 6 A       G            0.190  
# 7 A       H            0.0166 
# 8 A       I            0.297  
# 9 A       J            0.226  
#10 A       K            0.0193 
# ... with 266 more rows
Mankind_008
  • 2,158
  • 2
  • 9
  • 15
1

Here is another option based onyour data

library(tidyverse)

## data
m <- expand.grid(LETTERS[1:24],LETTERS[1:24],
                 stringsAsFactors = FALSE) # you should use stringsAsFactors = FALSE
m$weight <- runif(nrow(m), 0.01, max = 1)
#m <- m[m$Var1 != m$Var2, ] ##remove loop edges
m <- filter(m, Var1 != Var2) # filter also does the job
colnames(m) = c("to","from","weight")

## result 
m <- m %>%
        arrange(to) %>%
        mutate(edge = ifelse(to < from, paste(to, from, sep = ","),
                             paste(from, to, sep = ","))) %>%
        group_by(edge) %>%
        mutate(final_weight = abs(weight[1] - weight[2])) %>%
        select(edge, final_weight) %>%
        distinct() %>%
        separate(edge, c("to", "from"), ",")
nghauran
  • 6,648
  • 2
  • 20
  • 29