I have a graph, with multiple attributes, W1, W2, B1, B2, etc. I want to mutate the average attribute values of the neighbors.
I can write code for one attribute, but how could I use one operation to get all the averages of all the attributes and create separate variables for each neighbor's average attribute values?
create_ring(40) %>%
mutate(id = row_number()) -> ring
ring %>%
mutate(W1 = rnorm(40,75,15),
W2 = rnorm(40,20,11),
B1 = rnorm(40,2,11),
B2 = rnorm(40,1,1)) %>%
mutate(Neighs = local_members(order = 1,
mindist = 1),
k = local_size(order = 1,
mindist = 1)) -> ring
ring <- ring %>% mutate(W1_neighs = as.igraph(.) %>%
ego(order = 1, mindist = 1) %>%
sapply(function(v) mean(v$W1, na.rm=T)))```
I don't think need to use
as.igraph+egoto compute the mean of neighbors' attribute values. You can directly use the attribute values from the vertex dataframe and compute the means like belowand you will obtain