I haven't found a satisfying way (that I can ever remember) to do this kind of thing in igraph, so I always end up doing something likes the following.
First, here's some example data...
library(igraph, warn.conflicts = FALSE); set.seed(831); n_nodes <- 12
g <- random.graph.game(n_nodes, 0.2)
vertex_attr(g) <- list(name = letters[seq_len(n_nodes)],
sex = sample(c("male", "female"), n_nodes, replace = TRUE))
edge_attr(g) <- list(weight = sample(1:50, size = ecount(g)))
g
#> IGRAPH 8ef5eee UNW- 12 10 -- Erdos renyi (gnp) graph
#> + attr: name (g/c), type (g/c), loops (g/l), p (g/n), name (v/c), sex
#> | (v/c), weight (e/n)
#> + edges from 8ef5eee (vertex names):
#> [1] b--c f--g c--h f--h a--i b--i f--j e--k i--k c--l
... and here's a function that extracts networks containing only homophilous or heterophilous edges...
subgraph_edges_homophily <- function(graph, vattr_name, heterophily = FALSE,
drop_isolates = FALSE) {
stopifnot( # arg checks
igraph::is.igraph(graph) || is.character(vattr_name) ||
length(vattr_name) == 1L || !is.na(vattr_name) ||
vattr %in% igraph::vertex_attr_names(vattr_name)
)
vattrs <- igraph::vertex_attr(graph, name = vattr_name)
total_el <- igraph::as_edgelist(graph, names = FALSE)
# rows from total_el where the attribute of the edge source == attribute of edge target
edges_to_keep <- vattrs[total_el[, 1L]] == vattrs[total_el[, 2L]]
# for heterophilous ties, just negate the "in_group" version
if (heterophily) edges_to_keep <- !edges_to_keep
igraph::subgraph.edges(graph,
eids = which(edges_to_keep),
delete.vertices = drop_isolates)
}
subgraph_edges_homophily()
will let you extract the networks you're looking for like so...
# homophily
subgraph_edges_homophily(g, vattr_name = "sex")
#> IGRAPH 1bc4a38 UNW- 12 3 -- Erdos renyi (gnp) graph
#> + attr: name (g/c), type (g/c), loops (g/l), p (g/n), name (v/c), sex
#> | (v/c), weight (e/n)
#> + edges from 1bc4a38 (vertex names):
#> [1] e--k i--k c--l
# heterophily
subgraph_edges_homophily(g, vattr_name = "sex", heterophily = TRUE)
#> IGRAPH e79e82d UNW- 12 7 -- Erdos renyi (gnp) graph
#> + attr: name (g/c), type (g/c), loops (g/l), p (g/n), name (v/c), sex
#> | (v/c), weight (e/n)
#> + edges from e79e82d (vertex names):
#> [1] b--c f--g c--h f--h a--i b--i f--j
# no isolates
subgraph_edges_homophily(g, vattr_name = "sex", drop_isolates = TRUE)
#> IGRAPH 8ce3efe UNW- 5 3 -- Erdos renyi (gnp) graph
#> + attr: name (g/c), type (g/c), loops (g/l), p (g/n), name (v/c), sex
#> | (v/c), weight (e/n)
#> + edges from 8ce3efe (vertex names):
#> [1] e--k i--k c--l
... and you can then run metrics on those networks as desired.
Here's an example calculating between-class metrics like you were asking...
g %>%
subgraph_edges_homophily(vattr_name = "sex", heterophily = TRUE) %>%
betweenness(directed = FALSE)
#> a b c d e f g h i j k l
#> 0 10 12 0 0 11 0 12 6 0 0 0
-
sessionInfo()
#> R version 3.6.2 (2019-12-12)
#> Platform: x86_64-pc-linux-gnu (64-bit)
#> Running under: Ubuntu 18.04.4 LTS
#>
#> Matrix products: default
#> BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.7.1
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.7.1
#>
#> locale:
#> [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
#> [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8
#> [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8
#> [7] LC_PAPER=en_US.UTF-8 LC_NAME=C
#> [9] LC_ADDRESS=C LC_TELEPHONE=C
#> [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C
#>
#> attached base packages:
#> [1] stats graphics grDevices utils datasets methods base
#>
#> other attached packages:
#> [1] igraph_1.2.4.2
#>
#> loaded via a namespace (and not attached):
#> [1] compiler_3.6.2 magrittr_1.5 tools_3.6.2 htmltools_0.4.0
#> [5] yaml_2.2.1 Rcpp_1.0.3 stringi_1.4.6 rmarkdown_2.1.1
#> [9] highr_0.8 knitr_1.28 stringr_1.4.0 xfun_0.12
#> [13] digest_0.6.24 pkgconfig_2.0.3 rlang_0.4.4 evaluate_0.14