Cumulative value of an edge or node attribute while descending an igraph object

二次信任 提交于 2019-12-02 11:16:11

问题


I have an igraph object g made from dataframe df:

df <- data.frame(c(0,1,2,2,4), c(1,2,3,4,5), c(0.01, 0.03, 0.05, 0.01, 0.02))
colnames(df) <- c('parent_id', 'id', 'dt')
g <- graph_from_data_frame(df)

Edges are made between parent_id and id.

> g
IGRAPH DN-- 6 5 -- 
+ attr: name (v/c), dt (e/n)
+ edges (vertex names):
[1] 0->1 1->2 2->3 2->4 4->5

Change in thickness dt is the edge attribute. This can be thought of as the change in thickness between a 'parent' and 'child' iceberg (this is my problem/project).

list.edge.attributes(g)
[1] "dt"

to visualize:

plot(g, edge.label=E(g)$dt)

Example of nodes and edge attribute 'dt'

I need to find the cumulative sum of dt at each node while descending from parent to child.

When thinking in terms of 'ancestor', 'parent' and 'child' nodes, this is equivalent to getting the cumulative sum of dt for all ancestors at each 'child' node.

Cumulative dt assigned as edge attribute, anticipated outcome example

It is OK if these cumulative values are assigned as new node or edge attributes, or another form of output.

I have tried 1) the network.aggregate function in the RNewsflow package & 2) the aggregate function in the data.tree package.

Thank you in advance for interest and help.


回答1:


You can indeed use data.tree for this. Though Aggregate will sum up from children towards parent, and from what I understand, you want to do the opposite. So the following will work:

library(data.tree)
df <- get.data.frame(g, what = "edges")
dtr <- FromDataFrameNetwork(df)
dtr$dtcum <- 0
dtr$Do(function(node) node$dtcum <- node$parent$dtcum + node$dt, filterFun = isNotRoot)
print(dtr, "dt", "dtcum")

This will print out as:

          levelName   dt dtcum
1 0                   NA  0.00
2  °--1             0.01  0.01
3      °--2         0.03  0.04
4          ¦--3     0.05  0.09
5          °--4     0.01  0.05
6              °--5 0.02  0.07


来源:https://stackoverflow.com/questions/42474487/cumulative-value-of-an-edge-or-node-attribute-while-descending-an-igraph-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!