Subset (list of lists) nested Lists

大憨熊 提交于 2020-08-06 05:11:33

问题


I am trying to subset thead/tbody without directly calling rowlist$td$list$item$table$thead or rowlist[[td]][[list]][[item]][[table]][[thead]]. This unlist(rowlist, use.names=FALSE )[ grepl( "tbody", names(unlist(rowlist)))] serves my purpose except I need it as multiple rows (e.g. two tr's in tbody)(i can split it but seems counter intuitive . I know there should be a better way to work with HTML/XML but this is got I got for now.

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"

List looks like this

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"))

来源:https://stackoverflow.com/questions/63250082/subset-list-of-lists-nested-lists

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!