Kimberly
Kimberly

Reputation: 89

Discard Bipartite Edges on igraph in R

I am looking to discard bipartite edges on iGraph so that I am only seeing those with greater than 3 memberships. I am looking at a dataset of students who have attended classes. I want to only see a graph of classes where the connections are based on if there are more than 3 students per class.

students | class
Jane     | Biology
Jack     | Biology
Mark     | Biology
Steve    | Biology
Jane     | Chemistry
Jack     | Chemistry
Mark     | Chemistry
Steve    | Chemistry
Lane     | Physics
Steve    | Physiology

I want in the end to have a graph of just Biology and Chemistry with a line drawn between them as both these classes have a student membership > 3

Here is what I have so far:

g.students
cl<- clusters(g.students)
g.students<- delete.vertices(g.students, names(cl$membership[ cl$membership > 3]))

Somehow, when I input this, I am getting this back which can't be because there should be nodes and edges:

IGRAPH c2cf5c3 UN-B 1 0 -- 

+ attr: name (v/c), type (v/l)

I'm not sure why this is.. Is there a different way to discard these edges?

Upvotes: 1

Views: 81

Answers (1)

Julius Vainora
Julius Vainora

Reputation: 48241

Here's one way:

A <- as_adj(g.students) # Extracting the adjacency matrix
B <- as.matrix(A[unique(df$class), unique(df$students)]) # Selecting its relevant part
C <- B %*% t(B) # Numbers of common students between classes
C <- ifelse(C > 3, 1, 0) * (1 - diag(nrow(C))) # Setting diagonal to zero, discarding 
                                               # edges, and setting remaining weights to 1
C <- C[colSums(C) > 0, colSums(C) > 0]         # Discarding vertices
g <- graph_from_adjacency_matrix(C)            # Obtaining a graph

Upvotes: 1

Related Questions