1

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

Robin_oud
  • 11
  • 1

2 Answers2

0

From this answer: Equivalent of rowsum function for Matrix-class (dgCMatrix)

Matrix::rowSums(g[])
cardinal40
  • 1,245
  • 1
  • 9
  • 11
0

Thanks to people who answered!

After some thinking it seems that my question was poorly worded: I do not want to sum accross rows but to use row values to define a weights and index for a sum with values in other dataframe.

Anyway, I found my answer! Simple algebra was the way to go:

nodes$neigh_activity<-g[]%*%as.matrix(nodes$activity)

perfectly works.

And it is so much faster! The whole operation goes three times faster than ONE iteration of the loop...

Anyway thanks!

Robin_oud
  • 11
  • 1