简体   繁体   中英

Use apply to create a list of adjacency matrices from dataframe in R

I have an edgelist of friendships with 5 different schools over 3 waves. I'd like to create a list for each school that contains 3 adjacency matrices (one for each wave). I can do this one by one, but I would like to use a loop or an apply function to automate it.

This is the code I have used for one school and wave:

school1_w1 <- filter(edges, school == 1 & wave == 1) %>% 
          graph_from_data_frame(., directed = TRUE) %>%
          as_adjacency_matrix() %>% as.matrix()

school1_w2 <- filter(edges, school == 1 & wave == 2) %>% 
          graph_from_data_frame(., directed = TRUE) %>%
          as_adjacency_matrix() %>% as.matrix()

school1_w3 <- filter(edges, school == 1 & wave == 3) %>% 
          graph_from_data_frame(., directed = TRUE) %>%
          as_adjacency_matrix() %>% as.matrix()

school1 <- list(school1_w1, school1_w2, school1_w3)

How can I do this for all 5 schools with an apply or loop? Sample data below:

 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

We can use split + lapply :

library(igraph)

result <- lapply(split(edges, list(edges$school, edges$wave)), function(x) {
  graph_from_data_frame(x, directed = TRUE) %>%
    as_adjacency_matrix() %>% as.matrix()
})

Or with by :

result <- by(edges, list(edges$school, edges$wave), function(x) {
  graph_from_data_frame(x, directed = TRUE) %>%
    as_adjacency_matrix() %>% as.matrix()
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM