【问题标题】:Can a data.tree be built using other trees as child nodes in R?可以使用其他树作为 R 中的子节点来构建 data.tree 吗?
【发布时间】:2016-10-30 16:26:36
【问题描述】:

我正在从网页源中提取评论数据并使用以下方法构建树:

`tmpTree <- FromListExplicit(postData[[1]], nameName = "poster", childrenName = "child")`

其中postData 是使用xml_find_all 提取的节点列表,每次从列表中创建新树时,postData[[1]] 都会更改。提取节点的函数可以在我八月份发布的SO question 中找到,谢天谢地,RSelenium 的创建者自己回答了,jdharrison

我想问的是我是否可以创建一种树,例如:

newTree <- Node$new("Tree67770)
newTree$AddChild(tmpTree)

所以我最终得到一棵由其他三棵树组成的树,然后它们将成为最终树中的节点,当我绘制大树时,我可以看到所有名称(海报的)。

上述方法不起作用,错误cannot coerce type 'environment' to vector of type 'character' 是可以理解的,因为每个tmpTree 不是字符而是列表。我想把每棵树都改成一个 data.frame,然后把所有的 data.frame 添加回去来构建一棵大树,但在我看来它太长而且太麻烦了。任何帮助将不胜感激。谢谢。

已编辑以添加 dput 示例: 示例 1:

structure(list(postId = 2794984430, date = "Thursday, July 21, 2016 11:17 AM", 
poster = "MMM", disqusUname = "disqus_rVXuxnq9MP", message = "\rI am against abortion but I am in favour of contraceptives.  Is the MAP a (emergency) contraceptive or not?  Is the MAP abortive or not?  Unless there is clear unequivocal evidence about this, the circus will continue!\r", 
child = list(structure(list(postId = 2795948275, date = "Thursday, July 21, 2016 9:07 PM", 
    poster = "David Farrugia", disqusUname = "davidfarrugia", 
    message = "\rIt all depends when the soul has been installed into the egg. LOL\r"), .Names = c("postId", 
"date", "poster", "disqusUname", "message")))), .Names = c("postId", "date", "poster", "disqusUname", "message", "child"))

示例 2:

structure(list(postId = 2795142611, date = "Thursday, July 21, 2016 2:04 PM", 
poster = "David", disqusUname = "disqus_tTjwlqxma8", message = "\rthis reminds me of the Divorce debate.  the dinosaurs from church and the parliament seem to be against anything 'god' does not allow.  can they accept the fact that not all of us are into religious fairy tales?\r", 
child = list(structure(list(postId = 2796284665, date = "Friday, July 22, 2016 12:30 AM", 
    poster = "Nessy Testa", disqusUname = "NICOTI", message = "\rno they want to shove their \"morals\" down our throats.. then they go to repent their sins..\r"), .Names = c("postId", 
"date", "poster", "disqusUname", "message")))), .Names = c("postId", "date", "poster", "disqusUname", "message", "child"))

上述每个示例都会生成一棵具有根节点和子节点的树,并根据它们的大小进行选择,因为其他示例的深度为 6 层或更多。

我使用tmpTree &lt;- FromListExplicit(postData[[Example 1 or 2]], nameName = "poster", childrenName = "child") 提取树,然后尝试使用以下方法将其转换为新节点:

newTree <- Node$new("root6770")
newNode <- Node$new(tmpTree)
newTree$AddChildNode(newNode)

Error in as.vector(x, "character") : cannot coerce type 'environment' to vector of type 'character' 的结果是在 newNode &lt;- Node$new(tmpTree) 执行后立即执行的。

我希望通过这个例子更好地解释自己。感谢您的帮助。

【问题讨论】:

    标签: r tree


    【解决方案1】:

    是的,这是可能的。使用Node$AddChildNode 而不是Node$new 将子树添加到现有节点:

    library(data.tree)
    newTree <- Node$new("roottree")
    tmpTree <- Node$new("subtree")
    newTree$AddChildNode(tmpTree)
    

    【讨论】:

    • 谢谢,我回家后试试这个。顺便说一句,只是为了确保我理解正确,当您写 t2 &lt;- Node$new("subtree") 时,您指的是节点的实际名称还是整棵树?不会将t2 添加为子节点,只需将其添加为名称而无需其他信息吗?正如我所说,我稍后会尝试,并会告诉你我的结果。
    • 节点和树没有区别。树的入口点是根节点。任何其他节点都跨越一个子树。 AddChild 是创建Node 并使用AddChildNode 添加它的快捷方式。如果您使用AddChildNode,则添加的节点将被保留(及其所有属性和后代)。
    • 我已经按照建议进行了尝试,但是当我使用ts &lt;- Node$new(tmptree) 时仍然面临Error in as.vector(x, "character") : cannot coerce type 'environment' to vector of type 'character',所以很可能我一定做错了什么。当使用上面的示例时,新的子节点没有任何其他数据,因为subtree 只是一个字符串。我正在编辑我的问题,以便发布我的两棵(最小)树的 dput 示例,我试图将它们添加为同一棵树下的孩子,以期更好地解释自己。再次感谢。
    • 不要做Node$new(tmptree)Node$new() 创建一个新的 Node 对象并期望节点的名称作为参数。但是您的 tmptree 已经是 Node,您可以使用 AddChildNode 将其作为子树添加到任何其他节点,如上所示。
    • 好极了。我知道我在做一些愚蠢的事情。谢谢一百万。
    猜你喜欢
    • 2016-11-01
    • 2022-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-02-17
    • 1970-01-01
    • 2023-03-14
    • 2016-05-22
    • 2022-08-20
    相关资源
    最近更新 更多