【问题标题】:Subset (list of lists) nested Lists子集(列表列表)嵌套列表
【发布时间】:2020-11-24 17:43:46
【问题描述】:

我正在尝试子集 thead/tbody 而不直接调用 rowlist$td$list$item$table$t​​head 或 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 太大了,难以共享。

标签: r list subset


【解决方案1】:

如果必须将对象作为嵌套列表处理,一种方法是在rrapply-package 中使用rrapply(基础rapply 的扩展):

library(rrapply)  ## v1.2.1

out <- rrapply(rowlist, 
        classes = "list",
        condition = function(x, .xname) .xname %in% c("thead", "tbody"), 
        how = "flatten")

str(out, list.len = 2)
#> List of 2
#>  $ thead:List of 1
#>   ..$ tr:List of 7
#>   .. ..$ th:List of 1
#>   .. .. ..$ : chr "Test"
#>   .. ..$ th:List of 1
#>   .. .. ..$ : chr "Outcome"
#>   .. .. [list output truncated]
#>  $ tbody:List of 2
#>   ..$ tr:List of 7
#>   .. ..$ td:List of 1
#>   .. .. ..$ : chr "test1"
#>   .. ..$ td:List of 1
#>   .. .. ..$ : chr "High"
#>   .. .. [list output truncated]
#>   .. ..- attr(*, "ID")= chr "id_511"
#>   ..$ tr:List of 7
#>   .. ..$ td:List of 1
#>   .. .. ..$ : chr "test2"
#>   .. ..$ td:List of 1
#>   .. .. ..$ : chr "Low"
#>   .. .. [list output truncated]
#>   .. ..- attr(*, "ID")= chr "id_512"

这里,condition 函数仅返回名称为 theadtbody 的节点,how = "flatten" 返回平面列表中的节点(how = "prune" 将修剪保留原始列表结构的节点),以及 @ 987654330@ 不会跳过中间列表节点(就像基 rapply() 的情况一样)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 1970-01-01
    • 2020-07-18
    • 2018-09-21
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多