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.
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