Home > OS >  igraph: create multiple graphs and get network measures into dataframe
igraph: create multiple graphs and get network measures into dataframe

Time:10-13

I have an edgelist of friendship nominations from 5 schools over 3 waves. I'd like to get each ego's indegree, outdegree, and constraint values into a dataframe. I think I need to create a list of graph objects and then use apply or a loop to go through them and calculate each measure, but I'm not sure how to do that and get a dataframe output with each ego's id number.

Here is some sample data:

edges<-read.table(text=" ego  alter  wave  school
   1    4       1   1
   1    4       2   1
   1    3       3   1
   2    3       1   1
   2    4       2   1
   2    4       3   1
   3    1       1   1
   3    2       2   1
   3    3       3   1
   4    1       1   1
   4    1       2   1
   4    1       3   1
   5    8       1   2
   5    6       2   2
   5    7       3   2
   6    7       1   2
   6    7       2   2
   6    7       3   2
   7    8       1   2
   7    6       2   2
   7    6       3   2
   8    7       1   2
   8    7       2   2
   8    7       3   2
   9    10      1   3
   9    11      2   3
   9    12      3   3
  10    11      1   3
  10    11      2   3
  10    9       3   3
  11    12      1   3
  11    10      2   3
  11    12      3   3
  12    9       1   3
  12    10      2   3
  12    10      3   3
  13    14      1   4
  13    15      2   4
  13    16      3   4
  14    16      1   4
  14    16      2   4
  14    13      3   4
  15    16      1   4
  15    16      2   4
  15    16      3   4
  16    15      1   4
  16    15      2   4
  16    15      3   4
  17    20      1   5
  17    18      2   5
  17    18      3   5
  18    19      1   5
  18    20      2   5
  18    19      3   5
  19    17      1   5
  19    17      2   5
  19    17      3   5
  20    18      1   5
  20    17      2   5
  20    17      3   5", header = TRUE) 

Here's the data I want:

df <-read.table(text="student_id wave indegree outdegree constraint
         1    1        2         1  0.5555556
         1    2        1         1  1.0000000
         1    3        1         1  0.5000000
         2    1        0         1  1.0000000
         2    2        1         1  0.5000000
         2    3        0         1  1.0000000
         3    1        1         1  0.5000000
         3    2        0         1  1.0000000
         3    3        2         1  1.0000000
         4    1        1         1  1.0000000
         4    2        2         1  0.5555556
         4    3        1         1  0.5000000
         5    1        0         1  1.0000000
         5    2        0         1  1.0000000
         5    3        0         1  1.0000000
         6    1        0         1  1.0000000
         6    2        2         1  0.5555556
         6    3        1         1  1.0000000
         7    1        2         1  0.5555556
         7    2        2         1  0.5555556
         7    3        3         1  0.3750000
         8    1        2         1  0.5555556
         8    2        0         1  1.0000000
         8    3        0         1  1.0000000
        10    1        1         1  0.5000000
        10    2        2         1  0.5555556
        10    3        1         1  1.0069444
        11    1        1         1  0.5000000
        11    2        2         1  0.5555556
        11    3        0         1  1.0000000
        12    1        1         1  0.5000000
        12    2        0         1  1.0000000
        12    3        2         1  0.6111111
         9    1        1         1  0.5000000
         9    2        0         1  1.0000000
         9    3        1         1  1.0069444
        13    1        0         1  1.0000000
        13    2        0         1  1.0000000
        13    3        1         1  0.5000000
        14    1        1         1  0.5000000
        14    2        0         1  1.0000000
        14    3        0         1  1.0000000
        15    1        1         1  1.0000000
        15    2        2         1  0.5555556
        15    3        1         1  1.0000000
        16    1        2         1  0.5555556
        16    2        2         1  0.5555556
        16    3        2         1  0.5555556
        17    1        1         1  0.5000000
        17    2        2         1  0.6111111
        17    3        2         1  0.6111111
        18    1        1         1  0.5000000
        18    2        1         1  1.0069444
        18    3        1         1  1.0069444
        19    1        1         1  0.5000000
        19    2        0         1  1.0000000
        19    3        1         1  1.0069444
        20    1        1         1  0.5000000
        20    2        1         1  1.0069444
        20    3        0         1  1.0000000", header = TRUE) 

This is the really long code I used to get that data output above. Is there a way to do this will an apply or loop?

s1w1 <- graph_from_data_frame(d = filter(edges, school == 1 & wave == 1))
s1w2 <- graph_from_data_frame(d = filter(edges, school == 1 & wave == 2))
s1w3 <- graph_from_data_frame(d = filter(edges, school == 1 & wave == 3))


s2w1 <- graph_from_data_frame(d = filter(edges, school == 2 & wave == 1))
s2w2 <- graph_from_data_frame(d = filter(edges, school == 2 & wave == 2))
s2w3 <- graph_from_data_frame(d = filter(edges, school == 2 & wave == 3))


s3w1 <- graph_from_data_frame(d = filter(edges, school == 3 & wave == 1))
s3w2 <- graph_from_data_frame(d = filter(edges, school == 3 & wave == 2))
s3w3 <- graph_from_data_frame(d = filter(edges, school == 3 & wave == 3))

s4w1 <- graph_from_data_frame(d = filter(edges, school == 4 & wave == 1))
s4w2 <- graph_from_data_frame(d = filter(edges, school == 4 & wave == 2))
s4w3 <- graph_from_data_frame(d = filter(edges, school == 4 & wave == 3))

s5w1 <- graph_from_data_frame(d = filter(edges, school == 5 & wave == 1))
s5w2 <- graph_from_data_frame(d = filter(edges, school == 5 & wave == 2))
s5w3 <- graph_from_data_frame(d = filter(edges, school == 5 & wave == 3))



df1w1 <- data.frame(
  student_id = names(V(s1w1)),
  wave = 1,
  indegree = degree(s1w1, mode = "in"),
  outdegree = degree(s1w1, mode = "out"),
  constraint = constraint(s1w1) %>% unlist())
df1w2 <- data.frame(
  student_id = names(V(s1w2)),
  wave = 2,
  indegree = degree(s1w2, mode = "in"),
  outdegree = degree(s1w2, mode = "out"),
  constraint = constraint(s1w2) %>% unlist())
df1w3 <- data.frame(
  student_id = names(V(s1w3)),
  wave = 3,
  indegree = degree(s1w3, mode = "in"),
  outdegree = degree(s1w3, mode = "out"),
  constraint = constraint(s1w3) %>% unlist())


df2w1 <- data.frame(
  student_id = names(V(s2w1)),
  wave = 1,
  indegree = degree(s2w1, mode = "in"),
  outdegree = degree(s2w1, mode = "out"),
  constraint = constraint(s2w1) %>% unlist())
df2w2 <- data.frame(
  student_id = names(V(s2w2)),
  wave = 2,
  indegree = degree(s2w2, mode = "in"),
  outdegree = degree(s2w2, mode = "out"),
  constraint = constraint(s2w2) %>% unlist())
df2w3 <- data.frame(
  student_id = names(V(s2w3)),
  wave = 3,
  indegree = degree(s2w3, mode = "in"),
  outdegree = degree(s2w3, mode = "out"),
  constraint = constraint(s2w3) %>% unlist())


df3w1 <- data.frame(
  student_id = names(V(s3w1)),
  wave = 1,
  indegree = degree(s3w1, mode = "in"),
  outdegree = degree(s3w1, mode = "out"),
  constraint = constraint(s3w1) %>% unlist())
df3w2 <- data.frame(
  student_id = names(V(s3w2)),
  wave = 2,
  indegree = degree(s3w2, mode = "in"),
  outdegree = degree(s3w2, mode = "out"),
  constraint = constraint(s3w2) %>% unlist())
df3w3 <- data.frame(
  student_id = names(V(s3w3)),
  wave = 3,
  indegree = degree(s3w3, mode = "in"),
  outdegree = degree(s3w3, mode = "out"),
  constraint = constraint(s3w3) %>% unlist())


df4w1 <- data.frame(
  student_id = names(V(s4w1)),
  wave = 1,
  indegree = degree(s4w1, mode = "in"),
  outdegree = degree(s4w1, mode = "out"),
  constraint = constraint(s4w1) %>% unlist())
df4w2 <- data.frame(
  student_id = names(V(s4w2)),
  wave = 2,
  indegree = degree(s4w2, mode = "in"),
  outdegree = degree(s4w2, mode = "out"),
  constraint = constraint(s4w2) %>% unlist())
df4w3 <- data.frame(
  student_id = names(V(s4w3)),
  wave = 3,
  indegree = degree(s4w3, mode = "in"),
  outdegree = degree(s4w3, mode = "out"),
  constraint = constraint(s4w3) %>% unlist())


df5w1 <- data.frame(
  student_id = names(V(s5w1)),
  wave = 1,
  indegree = degree(s5w1, mode = "in"),
  outdegree = degree(s5w1, mode = "out"),
  constraint = constraint(s5w1) %>% unlist())
df5w2 <- data.frame(
  student_id = names(V(s5w2)),
  wave = 2,
  indegree = degree(s5w2, mode = "in"),
  outdegree = degree(s5w2, mode = "out"),
  constraint = constraint(s5w2) %>% unlist())
df5w3 <- data.frame(
  student_id = names(V(s5w3)),
  wave = 3,
  indegree = degree(s5w3, mode = "in"),
  outdegree = degree(s5w3, mode = "out"),
  constraint = constraint(s5w3) %>% unlist())


df <- list(df1w1, df1w2, df1w3, 
               df2w1, df2w2, df2w3,
               df3w1, df3w2, df3w3,
               df4w1, df4w2, df4w3,
               df5w1, df5w2, df5w3) %>% 
  reduce(full_join)

CodePudding user response:

Maybe you can try split lapply rbind like below

do.call(
  rbind,
  lapply(
    split(edges, ~ school   wave),
    function(x) {
      g <- graph_from_data_frame(x)
      data.frame(
        student_id = names(V(g)),
        wave = E(g)$wave,
        indegree = degree(g, mode = "in"),
        outdegree = degree(g, mode = "out"),
        constraint = constraint(g) %>% unlist()
      )
    }
  )
)

which gives

       student_id wave indegree outdegree constraint
1.1.1           1    1        2         1  0.5555556
1.1.2           2    1        0         1  1.0000000
1.1.3           3    1        1         1  0.5000000
1.1.4           4    1        1         1  1.0000000
2.1.5           5    1        0         1  1.0000000
2.1.6           6    1        0         1  1.0000000
2.1.7           7    1        2         1  0.5555556
2.1.8           8    1        2         1  0.5555556
3.1.9           9    1        1         1  0.5000000
3.1.10         10    1        1         1  0.5000000
3.1.11         11    1        1         1  0.5000000
3.1.12         12    1        1         1  0.5000000
4.1.13         13    1        0         1  1.0000000
4.1.14         14    1        1         1  0.5000000
4.1.15         15    1        1         1  1.0000000
4.1.16         16    1        2         1  0.5555556
5.1.17         17    1        1         1  0.5000000
5.1.18         18    1        1         1  0.5000000
5.1.19         19    1        1         1  0.5000000
5.1.20         20    1        1         1  0.5000000
1.2.1           1    2        1         1  1.0000000
1.2.2           2    2        1         1  0.5000000
1.2.3           3    2        0         1  1.0000000
1.2.4           4    2        2         1  0.5555556
2.2.5           5    2        0         1  1.0000000
2.2.6           6    2        2         1  0.5555556
2.2.7           7    2        2         1  0.5555556
2.2.8           8    2        0         1  1.0000000
3.2.9           9    2        0         1  1.0000000
3.2.10         10    2        2         1  0.5555556
3.2.11         11    2        2         1  0.5555556
3.2.12         12    2        0         1  1.0000000
4.2.13         13    2        0         1  1.0000000
4.2.14         14    2        0         1  1.0000000
4.2.15         15    2        2         1  0.5555556
4.2.16         16    2        2         1  0.5555556
5.2.17         17    2        2         1  0.6111111
5.2.18         18    2        1         1  1.0069444
5.2.19         19    2        0         1  1.0000000
5.2.20         20    2        1         1  1.0069444
1.3.1           1    3        1         1  0.5000000
1.3.2           2    3        0         1  1.0000000
1.3.3           3    3        2         1  1.0000000
1.3.4           4    3        1         1  0.5000000
2.3.5           5    3        0         1  1.0000000
2.3.6           6    3        1         1  1.0000000
2.3.7           7    3        3         1  0.3750000
2.3.8           8    3        0         1  1.0000000
3.3.9           9    3        1         1  1.0069444
3.3.10         10    3        1         1  1.0069444
3.3.11         11    3        0         1  1.0000000
3.3.12         12    3        2         1  0.6111111
4.3.13         13    3        1         1  0.5000000
4.3.14         14    3        0         1  1.0000000
4.3.15         15    3        1         1  1.0000000
4.3.16         16    3        2         1  0.5555556
5.3.17         17    3        2         1  0.6111111
5.3.18         18    3        1         1  1.0069444
5.3.19         19    3        1         1  1.0069444
5.3.20         20    3        0         1  1.0000000
  • Related