I have a graph, with an attribute W
. I want to mutate
the average W
of the neighbors. I want to keep the code as simple as possible, trying to avoid maps
and other explicit functions for vectorisation, because this is for didactic purposes.
My code, trying to show well each passage would look like this:
set.seed(1810)
create_ring(40) %>%
mutate(id = row_number()) -> ring
ring %>%
mutate(W = rnorm(40,75,15)) %>%
mutate(Neighs = local_members(order = 1,
mindist = 1),
k = local_size(order = 1,
mindist = 1)) -> ring
ring %>%
mutate(
W_neighs = map_dbl(
Neighs,
function(x) mean(W[id %in% unlist(x)])
)
)
Is there a way to perform these operations without recurring to map
? Tidygraph has a similar wrapper that is local_ave_degree
, that wraps igraph::knn
, that wraps something in C...
Also, I would be interested in an explanation if morph
can be useful for these operations.