使用R在数据树上聚合值 [英] Aggregating values on a data tree with R

查看:134
本文介绍了使用R在数据树上聚合值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试整理数据树结构中的小时数。我可以将小时数直接加到父节点下,但不能在树中包括分配给父节点的小时数。任何建议都会很棒。

I'm trying to tally up the hours from a data tree structure. I can add up the hours directly under parent node, but I can't include the hours assigned to the parent nodes in the tree. Any suggestions would be great.

levelName小时总小时
1 Ned NA 1
2°-约翰1 3
3°-凯特1 3
4 ¦--丹1 1
5 ¦- -罗恩1 1
6°-西耶娜1 1

levelName总小时数小时
1 Ned NA 5
2°-约翰1 5
3°-凯特1 4
4α-丹1 1
5β-罗恩1 1
6°-西耶娜1 1

# Install package
install.packages('data.tree')
library(data.tree)

# Create data frame
to <- c("Ned", "John", "Kate", "Kate", "Kate")
from <- c("John", "Kate", "Dan", "Ron", "Sienna")
hours <- c(1,1,1,1,1)
df <- data.frame(from,to,hours)

# Create data tree
tree <- FromDataFrameNetwork(df)
print(tree, "hours")

# Get running total of hours that includes all nodes and children values.
tree$Do(function(x) x$total <- Aggregate(x, "hours", sum), traversal = "post-order")
print(tree, "hours", runningtotal = tree$Get(Aggregate, "total", sum))


推荐答案

您可以简单地使用递归函数:

You could simply use a recursive function:

myApply <- function(node) {
  node$totalHours <- 
    sum(c(node$hours, purrr::map_dbl(node$children, myApply)), na.rm = TRUE)
}
myApply(tree)
print(tree, "hours", "totalHours")

结果:

           levelName hours totalHours
1 Ned                   NA          5
2  °--John               1          5
3      °--Kate           1          4
4          ¦--Dan        1          1
5          ¦--Ron        1          1
6          °--Sienna     1          1






编辑:填充两个元素:

# Create data frame
to <- c("Ned", "John", "Kate", "Kate", "Kate")
from <- c("John", "Kate", "Dan", "Ron", "Sienna")
hours <- c(1,1,1,1,1)
hours2 <- 5:1
df <- data.frame(from,to,hours, hours2)

# Create data tree
tree <- FromDataFrameNetwork(df)
print(tree, "hours", "hours2")

myApply <- function(node) {
  res.ch <- purrr::map(node$children, myApply)
  a <- node$totalHours <- 
    sum(c(node$hours,  purrr::map_dbl(res.ch, 1)), na.rm = TRUE)
  b <- node$totalHours2 <- 
    sum(c(node$hours2, purrr::map_dbl(res.ch, 2)), na.rm = TRUE)
  list(a, b)
}
myApply(tree)
print(tree, "hours", "totalHours", "hours2", "totalHours2")

结果:

           levelName hours totalHours hours2 totalHours2
1 Ned                   NA          5     NA          15
2  °--John               1          5      5          15
3      °--Kate           1          4      4          10
4          ¦--Dan        1          1      3           3
5          ¦--Ron        1          1      2           2
6          °--Sienna     1          1      1           1

这篇关于使用R在数据树上聚合值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆