我使用以下方式从网页的来源和建筑树中提取评论数据:
`tmpTree <- FromListExplicit(postData[[1]], nameName = "poster", childrenName = "child")`
其中postData
是使用xml_find_all
提取的节点列表,每次从列表创建新树时都会更改postData[[1]]
。提取节点的函数可以在我8月份发布的SO question中找到,幸好由RSelenium自己的创建者jdharrison回答。
我想问的是,我是否可以创建一种树木,例如:
newTree <- Node$new("Tree67770)
newTree$AddChild(tmpTree)
这样我最终会得到一棵由其他三棵树组成的树,这些树将成为最终树中的节点,当我绘制大树时,我可以看到所有的名字(海报&#39; s)。
以上操作不起作用且错误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 <- 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 <- Node$new(tmpTree)
的结果。
我希望通过这个例子我能更好地解释自己。谢谢你的帮助。
答案 0 :(得分:2)
是的,这是可能的。使用Node$AddChildNode
代替Node$new
将子树添加到现有节点:
library(data.tree)
newTree <- Node$new("roottree")
tmpTree <- Node$new("subtree")
newTree$AddChildNode(tmpTree)