 # How to subgraph a network based on sex?

I have been trying to subgraph a population-wide network into 2 smaller subgraphs each consisting of only males and females in order to visualize sex-linked sociality and other various network measurements across the sexes.

However, I cannot seem to get the subgraph function to work properly on my data
My node attributes are stored as “Sex” inside the network as an either “M” or “F”

I have used an older textbook that stated I should run:

``````Female_network <- get.inducedSubgraph(myNetwork,
which (myNetwork %>% "Sex" == F))
``````

and I understand `get.inducedSubgraph` has depreciated.

I have also tried

``````Female_network <- subgraph(myNetwork, My_network\$Sex[["F"]]
``````

which also has been deprecated in favor of `igraph_induced_subgraph`. However, when that function is run it can also not be found.

I’m sure I’m missing a very basic command to do this but I can’t seem to get anything to work. Any help would be greatly appreciated

The function you are looking for is simply called `induced_subgraph()` in R. The error message that you see when calling `subgraph()` is a bit misleading; it comes from the C core of igraph, and in the C core the function is called `igraph_induced_subgraph()`. So, a full example:

``````library(igraph)

# Let's create an example graph first
g <- grg.game(100, 0.2)

# Assign gender randomly
V(g)\$gender <- sample(c("M", "F"), vcount(g), replace=T)

# Now take the female subgraph
sg <- induced_subgraph(g, V(g)\$gender == "F")

# and print the gender attribute for confirmation
> V(sg)\$gender
 "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
 "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"
 "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F" "F"``````