R:在不改变的情况下修剪 data.tree [英] R: Pruning data.tree without altering

查看:64
本文介绍了R:在不改变的情况下修剪 data.tree的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data.tree 包中,当修剪一棵树时,它会永久改变这棵树.这是有问题的,因为我的 data.tree 需要很长时间来生成,而且我不想每次必须进行新的修剪时都生成一个新的.

In the data.tree package when one prunes a tree, it permanently alters the tree. This is problematic, as my data.tree takes a long time to generate, and I don't want to generate a new one everytime I have to do a new pruning.

这里我生成了一个 data.tree

Here I generate a data.tree

# Loading data and library
library(data.tree)
data(acme)

# Function to add cumulative costs to all nodes
Cost <- function(node) {
  result <- node$cost
  if(length(result) == 0) result <- sum(sapply(node$children, Cost))
  return (result)
}

# Adding costs and other data for my example
acme$Do(function(node) node$cost <- Cost(node), filterFun = isNotLeaf)
acme$IT$Outsource$AddChild("Tester Inc")
acme$IT$Outsource$`Tester Inc`$cost <- 10
print(acme, "p", "cost")
                          levelName    p    cost
1  Acme Inc.                          NA 4950000
2   ¦--Accounting                     NA 1500000
3   ¦   ¦--New Software             0.50 1000000
4   ¦   °--New Accounting Standards 0.75  500000
5   ¦--Research                       NA 2750000
6   ¦   ¦--New Product Line         0.25 2000000
7   ¦   °--New Labs                 0.90  750000
8   °--IT                             NA  700000
9       ¦--Outsource                0.20  400000
10      ¦   °--Tester Inc             NA      10
11      ¦--Go agile                 0.05  250000
12      °--Switch to R              1.00   50000

我在这里修剪树.

# Pruner function
Pruner <- function(node) {
  cost <- node$cost
  cost_parent <- node$parent$cost
  if(cost < 2800000 & cost_parent > 2800000) {
    return(TRUE)
  } else {
    return(FALSE)
  }
}

# Pruning the tree
Prune(acme, function(node) Pruner(node))
print(acme, "p", "cost")
       levelName  p    cost
1 Acme Inc.      NA 4950000
2  ¦--Accounting NA 1500000
3  ¦--Research   NA 2750000
4  °--IT         NA  700000

我尝试以多种方式保存我的 data.tree 对象,但它们最终都会生成巨大的文件,或者比从头开始生成新树花费的时间更长.

I have tried to save my data.tree object in several ways, but they all end up generating HUGE files or by taking longer than it would to generate a new tree from the scratch.

# Saving object
save(acme, file = "acme.RData")
saveRDS(acme, "acme.rds")

# Generating a clone
acme_clone <- Clone(acme)

我的下一个直觉是看看我是否可以使用 Get 函数临时修剪树,因为 data.tree 文档指出 这有两种变体:临时修剪,例如仅用于打印:这是 pruneFun 参数,例如在获取副作用或永久修剪中,这意味着您可以永久修改 data.tree 结构.这是通过修剪方法实现的.

My next intuition was to see if I could just temporarily prune the tree using the Get function, as the data.tree documentation states that There are two variations of this: temporary pruning, e.g. just for printing: This is the pruneFun parameter, e.g. in Get side effect or permanent pruning, meaning that you modify your data.tree structure for good. This is achieved with the Prune method.

由于没有示例,因此不清楚如何进行这项工作.

It was not clear how to make this work as there were no examples.

推荐答案

经过一番折腾之后,我终于尝试了以下方法,并使其成功.没有很好的例子,所以我想我会在这里留下一个.

After some fiddeling around I finally tried the following, and made it work. There are no good examples, so I thought I would leave one here.

print(acme, "cost", pruneFun = function(node) Pruner(node)) 
       levelName    cost
1 Acme Inc.      4950000
2  ¦--Accounting 1500000
3  ¦--Research   2750000
4  °--IT          700000

这篇关于R:在不改变的情况下修剪 data.tree的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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