Obed
Obed

Reputation: 423

Create grouped edge lists

I have a list of people and I want to examine the characteristics of their collaborative networks. However, I want to use all the edges of their networks, not just the edges that are directly connected to them. For example, let's say that I am looking at Bob's network. For Project1, Bob, Bill, and Jane worked together.

If I only cared about the edges connected to Bob, then my edge list would be: Bob--Bill, Bob--Jane. The network would have 0 triangles. However, if I looked at all the edges for Bob's network, the edge list would look like this: Bob--Bill, Bob--Jane, Bill--Jane. The network would have 1 triangle.

The only way to do this that I can think of is to create a "grouped" edge list. I'm just not sure how to tackle it.

My edge data looks like this:

collab <- data.frame(vertex1 = c("Bob","Bill","Bob","Jane","Bill","Jane","Bob","Jane","Bob","Bill","Bob"
                                  ,"Jane","Bill","Jane","Bob","Jane","Jane","Jill","Jane","Susan","Susan"),
                      edgeID = c(1,1,1,1,1,1,2,2,1,1,1,1,1,1,2,2,3,3,3,3,3),
                      vertex2 = c("Bill","Bob","Jane","Bob","Jane","Jill","Jane","Bob","Bill","Bob"
                                  ,"Jane","Bob","Jane","Bill","Jane","Bob","Jill","Jane","Susan","Jane","Jill"))
   vertex1 edgeID vertex2
1      Bob      1    Bill
2     Bill      1     Bob
3      Bob      1    Jane
4     Jane      1     Bob
5     Bill      1    Jane
6     Jane      1    Jill
7      Bob      2    Jane
8     Jane      2     Bob
9     Jane      3    Jill
10    Jill      3    Jane
11    Jane      3   Susan
12   Susan      3    Jane
13   Susan      3    Jill

I have another vector that holds the names of my target people:

targets <- data.frame(name=c("Bob","Jane"))

What I'd like to do is group the relevant edges under the appropriate targets such that the result is something like:

   group vertex1 edgeID vertex2
1    Bob     Bob      1    Bill
2    Bob    Bill      1     Bob
3    Bob     Bob      1    Jane
4    Bob    Jane      1     Bob
5    Bob    Bill      1    Jane
6    Bob    Jane      1    Jill
7    Bob     Bob      2    Jane
8    Bob    Jane      2     Bob
9   Jane     Bob      1    Bill
10  Jane    Bill      1     Bob
11  Jane     Bob      1    Jane
12  Jane    Jane      1     Bob
13  Jane    Bill      1    Jane
14  Jane    Jane      1    Bill
15  Jane     Bob      2    Jane
16  Jane    Jane      2     Bob
17  Jane    Jane      3    Jill
18  Jane    Jill      3    Jane
19  Jane    Jane      3   Susan
20  Jane   Susan      3    Jane
21  Jane   Susan      3    Jill

I figure that if I can get here, I can make a for loop that cycles through each target, creates a graph with iGraph, and calculates the network metrics for Bob and Jane without much issue. Am I going about this the right way or does iGraph have a better way to do this?

Upvotes: 2

Views: 62

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101257

Here might be one option

g <- graph_from_data_frame(collab[c(1, 3, 2)], directed = FALSE)
do.call(
  rbind,
  c(
    make.row.names = FALSE,
    lapply(
      targets$name,
      function(nm) {
        z <- c(nm, V(g)$name[distances(g, nm) == 1])
        cbind(group = nm, unique(subset(collab, vertex1 %in% z & vertex2 %in% z)))
      }
    )
  )
)

which gives

   group vertex1 edgeID vertex2
1    Bob     Bob      1    Bill
2    Bob    Bill      1     Bob
3    Bob     Bob      1    Jane
4    Bob    Jane      1     Bob
5    Bob    Bill      1    Jane
6    Bob     Bob      2    Jane
7    Bob    Jane      2     Bob
8    Bob    Jane      1    Bill
9   Jane     Bob      1    Bill
10  Jane    Bill      1     Bob
11  Jane     Bob      1    Jane
12  Jane    Jane      1     Bob
13  Jane    Bill      1    Jane
14  Jane    Jane      1    Jill
15  Jane     Bob      2    Jane
16  Jane    Jane      2     Bob
17  Jane    Jane      1    Bill
18  Jane    Jane      3    Jill
19  Jane    Jill      3    Jane
20  Jane    Jane      3   Susan
21  Jane   Susan      3    Jane
22  Jane   Susan      3    Jill

Upvotes: 1

Related Questions