我会自己进行投影,并使用自己full_join()
的 edgelist 进行投影,然后使用它igraph::simplify()
来保留交易编号:
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