I have a list of ego networks, and my goal is to get the density of alters only. So essentially removing ego from the density calculation.
Using the same sample data from my previous question, I can use that answer to get a list of ego network densities, but how can not include each ego in that calculation?
Data:
df<-read.table(text="student_id alter
1 3
2 5
2 2
2 3
3 2
3 4
3 5
4 1
4 6
4 3
5 1
5 6
5 2
6 5
6 2
6 1
7 8
8 9
9 8
9 7
10 7
10 9
11 19
11 15
11 12
12 20
12 19
12 11
13 15
13 19
13 11
14 16
14 12
14 18
15 17
15 20
15 17
16 14
16 19
16 20
16 13
17 20
17 18
17 13
17 14
18 13
18 19
18 17
19 17
19 16
19 11
20 13
20 17
20 11
20 1", header = TRUE)
Current code:
g <- graph_from_data_frame(df, directed = TRUE)
egonet_list <- make_ego_graph(g, order = 1, mode = "out")
dat <- data.frame(
student_id = names(V(g)),
egonet_density = lapply(egonet_list, graph.density) %>% unlist()
)
But my desired output, would mean deleting ego from the density calculation to get only the density of alters.
UPDATE
For example, with student_id 18 in the data example:
egonet_list[[18]] %>% delete.vertices(., "18") %>% graph.density()
Where I'm struggling is how to do this within an apply or loop so it can run through each ego network and do this.