【发布时间】:2020-11-24 17:43:46
【问题描述】:
我正在尝试子集 thead/tbody 而不直接调用 rowlist$td$list$item$table$thead 或 rowlist[[td]][[list]][[item]][[table]][[thead ]]。这
unlist(rowlist, use.names=FALSE )[ grepl( "tbody", names(unlist(rowlist)))] 服务于我的目的,除了我需要它作为多行(例如 tbody 中的两个 tr)(我可以拆分它,但似乎违反直觉。
我知道应该有更好的方法来处理 HTML/XML,但我现在已经得到了。
str(rowlist)
List of 1
$ td:List of 1
..$ list:List of 1
.. ..$ item:List of 1
.. .. ..$ table:List of 2
.. .. .. ..$ thead:List of 1
.. .. .. .. ..$ tr:List of 7
.. .. .. .. .. ..$ th:List of 1
.. .. .. .. .. .. ..$ : chr "Test"
.. .. .. .. .. ..$ th:List of 1
.. .. .. .. .. .. ..$ : chr "Outcome"
.. .. .. .. .. ..$ th:List of 1
.. .. .. .. .. .. ..$ : chr "Subset"
.. .. .. .. .. ..$ th:List of 1
.. .. .. .. .. .. ..$ : chr "Cups"
.. .. .. .. .. ..$ th:List of 1
.. .. .. .. .. .. ..$ : chr "Bowls"
.. .. .. .. .. ..$ th:List of 1
.. .. .. .. .. .. ..$ : chr "Plates"
.. .. .. .. .. ..$ th:List of 1
.. .. .. .. .. .. ..$ : chr "Jars"
.. .. .. ..$ tbody:List of 2
.. .. .. .. ..$ tr:List of 7
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "test1"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "High"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Low"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Gold"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Blue"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Green"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "red"
.. .. .. .. .. ..- attr(*, "ID")= chr "id_511"
.. .. .. .. ..$ tr:List of 7
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "test2"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Low"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "High"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Pink"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Blue"
.. .. .. .. .. ..$ td:List of 1
.. .. .. .. .. .. ..$ : chr "Purple"
.. .. .. .. .. ..$ td: list()
.. .. .. .. .. ..- attr(*, "ID")= chr "id_512"
.. ..- attr(*, "styleCode")= chr "none"
列表看起来像这样
rowlist<-list(td = structure(list(list = structure(list(item = list(table = list(
thead = list(tr = list(
th = list("Test"), th = list("Outcome"), th = list("Set"), th = list("Cups"), th = list("Bowls"), th = list( "Plates"), th = list("Jars"))),
tbody = list(tr = structure(
list(td = list("test1"), td = list("High"), td = list("Low"), td = list("Gold"), td = list("Blue"), td = list("Green"), td = list("Red")), ID = "id_511"),
tr = structure(
list(td = list("test2"), td = list("Low"), td = list("High"), td = list("Pink"), td = list("Blue"), td = list("Purple"), td = list()), ID = "id_512"))))), styleCode = "none")), colspan = "20"))
【问题讨论】:
-
如果您包含一个简单的reproducible example,其中包含可用于测试和验证可能解决方案的示例输入和所需输出,则更容易为您提供帮助。使用 `dput()` 而不是
str(),这样我们就可以将数据复制/粘贴到 R 中。此外,列表之前的数据是什么?它是一个html文档吗?使用 html 解析器可能会更容易导航和子集数据,而不是使用嵌套列表。 -
这是一个 XML 文档中的 HTML 表,xml2 as_list 让我可以更轻松地通过循环访问某些数据,例如而不是 test/some/other/thing 我可以做 test[[3]][[2]][[4]]
-
当
xml2时,您可以使用 xpath 表达式轻松选择节点。列表列表有点混乱。您可以共享 HTML/XML 数据吗? -
节点选择已经成为一个问题,因为一些节点具有相同的名称并且两者相似的不同内容被严重混淆,这不是一个常规的 XML doc 边缘或 xml 规则,列表确实使映射对我来说更加简单。 xml 太大了,难以共享。