简体   繁体   中英

In R package igraph, how can I normalize an edge attribute by dividing the edge attribute by a node attribute of the out-node?

Basically how to apply division by same node attribute to all edges that has that node as the one from which the edge originates. For example, this can be used to row-normalize an adjacency matrix.

Assuming that the weights are in an edge attribute named weight , you can do this:

E(g)$weight / strength(g, mode="out")[get.edgelist(g)[,1]]

The basic idea is that strength(g) gives for each vertex the sum of the weights of the edges incident on it. mode="out" ensures that only outbound edges are considered. Then you index the strength vector with the first column of the edge list (ie the source vertex of each edge) to get a vector where each element belongs to an edge and tells the total weight of its source vertex. Then you divide the actual weight vector with this in an elementwise manner.

If the weight is in another attribute, you have to pass the name of that attribute to strength() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM