繁体   English   中英

子集(列表列表)嵌套列表

[英]Subset (list of lists) nested Lists

我试图在不直接调用 rowlist$td$list$item$table$thead 或 rowlist[[td]][[list]][[item]][[table]][[thead]] 的情况下对 thead/tbody 进行子集化。 这个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"))

如果 object 必须作为嵌套列表处理,一种方法是在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 function 仅返回名称为theadtbody的节点, how = "flatten"返回平面列表中的节点( how = "prune"将修剪保留原始列表结构的节点),而classes = "list"确实不要跳过中间列表节点(就像基本rapply()的情况一样)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM