1

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.

ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81
L. Tucker
  • 523
  • 2
  • 12

1 Answers1

1

We can try the code below

dat <- data.frame(
  student_id = names(V(g)),
  egonet_density = mapply(function(gnet, v) graph.density(delete.vertices(gnet, v)), egonet_list, as.list(names(V(g))))
)

which gives

> dat
   student_id egonet_density
1           1            NaN
2           2      0.5000000
3           3      0.5000000
4           4      0.3333333
5           5      0.5000000
6           6      0.6666667
7           7            NaN
8           8            NaN
9           9      0.5000000
10         10      0.5000000
11         11      0.1666667
12         12      0.5000000
13         13      0.5000000
14         14      0.0000000
15         15      1.0000000
16         16      0.1666667
17         17      0.2500000
18         18      0.5000000
19         19      0.0000000
20         20      0.1666667
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81