【问题标题】:Recursive aggregation of data tree数据树的递归聚合
【发布时间】:2020-11-18 17:16:46
【问题描述】:

我正在使用 data.tree 包来执行分析,而不是嵌套列表,因为我的实际数据最终导致列表过于嵌套并且很难处理。

我根据这个问题整理了样本数据。 Aggregating values on a data tree with R。 在我自己的数据中,我有 to, from, hours 数据,需要创建 actual_hours 数据,这是从其父节点的 hours 值中减去的子节点的总和。

library(data.tree)
#Have
to <- c("Team1", "Team1-1","Team1-1", "Team1-1-1", "Team1-1-1", "Team1-1-1", "Team1-1-2")
from <- c("Team1-1", "Team1-1-1","Team1-1-2", "Team1-1-1a", "Team1-1-1b", "Team1-1-1c" ,"Team1-1-2a")
hours <- c(NA,150,200,65,20,30, 30)
df <- data.frame(from,to,hours)

# Create data tree
tree <- FromDataFrameNetwork(df)
###Current Output
print(tree, "hours")
              levelName hours
1 Team1                     NA
2  °--Team1-1               NA
3      ¦--Team1-1-1        150
4      ¦   ¦--Team1-1-1a    65
5      ¦   ¦--Team1-1-1b    20
6      ¦   °--Team1-1-1c    30
7      °--Team1-1-2        200
8          °--Team1-1-2a    30


#Need to create
actual_hours <- c(NA,35,170, 65,20,30, 30)
df <- data.frame(from,to,hours, actual_hours)
# Create data tree
tree <- FromDataFrameNetwork(df)
#Desired output
print(tree, "hours", 'actual_hours')

               levelName hours actual_hours
1 Team1                     NA           NA
2  °--Team1-1               NA           NA
3      ¦--Team1-1-1        150           35
4      ¦   ¦--Team1-1-1a    65           65
5      ¦   ¦--Team1-1-1b    20           20
6      ¦   °--Team1-1-1c    30           30
7      °--Team1-1-2        200          170
8          °--Team1-1-2a    30           30

我不确定该怎么做,因为它涉及树的向上和向下移动?我认为使用树的height 和/或level 属性是可行的方法,但不确定。

【问题讨论】:

  • 我已经为这个问题提供了一个解决方案,该解决方案涉及将树结构转换回数据框,但将树级别作为变量包括在内,并使用该变量来创建所需的数据。等我整理好了再发。

标签: r tree aggregate nodes data.tree


【解决方案1】:

假设 data.tree 看起来像这样

               levelName hours
1 Team1                     NA
2  °--Team1-1                0
3      ¦--Team1-1-1        150
4      ¦   ¦--Team1-1-1a    65
5      ¦   ¦--Team1-1-1b    20
6      ¦   °--Team1-1-1c    30
7      °--Team1-1-2        200
8          °--Team1-1-2a    30

这里有两个版本供您试用,因为我不确定您想要哪个版本。


非累积

tree$Do(function(node) {
  node$actual_hours <- node$hours - if (node$isLeaf) 0 else Aggregate(node, attribute = "hours", aggFun = sum)
}, traversal = "post-order")
> print(tree, "hours", "actual_hours")
               levelName hours actual_hours
1 Team1                     NA           NA
2  °--Team1-1                0         -350 # see here, -350=0-(150+200)
3      ¦--Team1-1-1        150           35
4      ¦   ¦--Team1-1-1a    65           65
5      ¦   ¦--Team1-1-1b    20           20
6      ¦   °--Team1-1-1c    30           30
7      °--Team1-1-2        200          170
8          °--Team1-1-2a    30           30

累积

tree$Do(function(node) {
  node$actual_hours <- node$hours - if (node$isLeaf) 0 else Aggregate(node, attribute = "actual_hours", aggFun = sum)
}, traversal = "post-order")
> print(tree, "hours", "actual_hours")
               levelName hours actual_hours
1 Team1                     NA           NA
2  °--Team1-1                0         -205 # -205=0-(35+170)
3      ¦--Team1-1-1        150           35
4      ¦   ¦--Team1-1-1a    65           65
5      ¦   ¦--Team1-1-1b    20           20
6      ¦   °--Team1-1-1c    30           30
7      °--Team1-1-2        200          170
8          °--Team1-1-2a    30           30

【讨论】:

  • 今天把它付诸实践,比我在尝试转换回数据帧并使用树级别执行聚合的过程中更有效的解决方案。这是我正在寻找的累积版本。谢谢
猜你喜欢
  • 2021-08-25
  • 2016-02-24
  • 2014-03-01
  • 2014-04-09
  • 2014-03-23
  • 2018-01-11
  • 2014-04-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多