I didn't find an answer to my question although it does seem easy - if this has been answered please forgive me. I am working with directed weighted network data and I want to compute the ''exposition'' of each node to some attribute of their outgoing neighbors by summing the value for each neighbor and weighting by the weights of edges.
This is pretty easy to do with a loop. g[] being the adjacency matrix I get:
library(igraph)
#Data example
relations <- data.frame(from=c("Bob", "Cecil", "Cecil", "David", "David", "Esmeralda"),
to=c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
weight=c(4,5,5,2,1,1))
g <- graph.data.frame(relations, directed=TRUE)
# Loop
for (i in c(1:nrow(g[]))){
nodes$neigh_activity[[i]] <- sum(nodes[which(g[i,]>0), ]$activity*g[i, which(g[i,]>0)])
}
I want to vectorize this loop. Indeed, in my actual data, I have over 90,000 nodes so it takes a while... I hear that vectorizing can greatly reduce computation time.
I am not sure of the way to go since I cannot input a vector into g[i,]
otherwise the sum breaks. I tried using split and work with list of vectors but this seems even more inefficient.
Thank you so much! Robin