0

I have a quick question about retaining node information after converting to a one mode projection.

library(igraph)
data <- data.frame( company=c(letters[1:5],letters[5:8],"a"), Deal=c(14,14,15,15,16,16,17,17,18,18))
g <- graph_from_data_frame(data)
V(g)$type <-  V(g)$name %in% data[,1]
proj <- bipartite.projection(g)
proj$proj2

I want to use the company to company ties as my new edgelist but retain the Deal numbers as an edge attribute so that I would ideally have a new dataset that looks like this:

Source Target Deal
a b 14
c d 15
f g 17
h a 18

where "Source", "Target", and "Deal" are each in their own column. (Sorry this does not look prety!)

I can create a dataframe with source and target, but am have a hard time figuring out how to add deal back in the third column. Any advice or guidance would be greatly appreciated! Here is the code I am using:

el00<-as_edgelist(proj$proj2)
colnames(el00) <- c("Source", "Target")     



   
Holly L
  • 23
  • 1
  • 4
  • Hi all, I am still wrestling with this issue. If anyone has any ideas, I would greatly appreciate your input! Thank you! – Holly L Aug 31 '20 at 01:20

1 Answers1

0

I would do the projection myself with full_join()ing the edgelist with itself and then use igraph::simplify() to preserve the Deal numbers:

library(igraph)
#> 
#> Attaching package: 'igraph'
#> The following objects are masked from 'package:stats':
#> 
#>     decompose, spectrum
#> The following object is masked from 'package:base':
#> 
#>     union
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:igraph':
#> 
#>     as_data_frame, groups, union
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
data <- data.frame( 
  company=c(letters[1:5],letters[5:8],"a", "a", "b"), 
  Deal=c(14,14,15,15,16,16,17,17,18,18,19,19)
)

# Project onto company mode "by hand". This is a multigraph, i.e. there might be
# mulitple edges (with different deal numbers) between a pair of companies.
edb <- data %>%
  full_join(data, by = "Deal") %>%
  filter(company.x < company.y) %>%
  select(
    v1 = company.x,
    v2 = company.y,
    Deal
  )
edb
#>   v1 v2 Deal
#> 1  a  b   14
#> 2  c  d   15
#> 3  f  g   17
#> 4  a  h   18
#> 5  a  b   19

# Make the graph and simplify preserving the `Deal` as the unique deal numbers
g <- edb %>%
  graph_from_data_frame(directed=FALSE) %>%
  simplify(
    edge.attr.comb = unique
  )
# The edge attribute Deal is a list of deal numbers. E.g. companies 'a' and 'b'
# are engaged together in deals 14 and 19.
E(g)$Deal
#> [[1]]
#> [1] 14 19
#> 
#> [[2]]
#> [1] 18
#> 
#> [[3]]
#> [1] 15
#> 
#> [[4]]
#> [1] 17
Michał
  • 2,755
  • 1
  • 17
  • 20