简体   繁体   English

在嵌套列表中查找包含子元素的顶级元素

[英]Find top-level element that contains a sub-element in a nested list

I would like to get a list of top level elements that contain a particular sub-element. 我想获取包含特定子元素的顶级元素的列表。 Suppose I have a nested list: 假设我有一个嵌套列表:

v <- list(A=list('a', 'b', 'c'), B=list('c','d','e'), C=list('d'))

I am trying to get the list of all top-level elements that have a particular sub element: 我正在尝试获取具有特定子元素的所有顶级元素的列表:

  • if I am looking for 'c' I would expect to get ('A', 'B'). 如果我正在寻找“ c”,我期望得到(“ A”,“ B”)。
  • if I am looking for 'd' I would expect to get ('B', 'C') 如果我正在寻找“ d”,我期望得到(“ B”,“ C”)

NOTE: prefer a base-R solution but would be interesting to see others 注意:更喜欢使用base-R解决方案,但是看到其他人会很有趣

You can use Filter : 您可以使用Filter

names(Filter(function(x) "c" %in% x,v))
# [1] "A" "B"
names(Filter(function(x) "d" %in% x,v))
# [1] "B" "C"

Or with library purrr : 或使用库purrr

names(purrr::keep(v, ~"c" %in% .))
# [1] "A" "B"
names(purrr::modify_if(v, ~!"c" %in% ., ~NULL))
# [1] "A" "B"

sapply替代Filter

names(v)[sapply(v, function(x) 'c' %in% x)]

Windows R CMD 检查顶级文件:(README)[警告]此文档格式需要非空<title>元素&lt;/div&gt;</title><div id="text_translate"><p> 我已在 Win、OSX 和 Linux 上为 R 激活 GitHub 对 CI 的操作。 在 Windows 上只发生以下警告(变成错误)。</p><pre> * checking top-level files... WARNING Conversion of 'README.md' failed: [WARNING] This document format requires a nonempty &lt;title&gt; element. Please specify either 'title' or 'pagetitle' in the metadata, eg by using --metadata pagetitle="..." on the command line. Falling back to 'README'</pre><p> <a href="https://github.com/UBESP-DCTV/covid19ita/pull/62/checks?check_run_id=594799232" rel="nofollow noreferrer">这里</a>是完整的报告。</p><p> 似乎它是由 CMD 直接检查调用的,因此我无法更改对 pandoc 的调用。</p><p> 另一方面,我尝试在README.Rmd header 中包含一个 pandoc 参数,正如<a href="https://community.rstudio.com/t/rmarkdown-document-format-requires-a-nonempty-title-element/36774" rel="nofollow noreferrer">这里</a>所建议的那样:</p><pre> output: github_document: pandoc_args: "--number-offset=1,0" toc: true pagetitle: covid19ita</pre><p> 它没有效果。</p><p> 注意:我不会从 CMD 检查中删除error_on = "warning" 。</p><p> 我该怎么做才能使测试也通过胜利?</p></div> - Windows R CMD check top-level files: (README) [WARNING] This document format requires a nonempty <title> element

暂无
暂无

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

相关问题 嵌套列表:对列表中每个子元素的不同元素应用不同的功能 - Nested list: applying different functions over different elements of each sub-element in list 如何使用列表子元素名称而不是实际的子元素名称 - How to use list sub-element names instead of actual sub-element name 如何按顺序命名列表的每个元素和子元素? - How to name each element and sub-element of a list in a sequential order? 如何使用 $ 标记获取 dplyr pipe 中列表的子元素 - how to use $ mark to get a sub-element of a list in dplyr pipe 获取R对象的每个子元素 - Get every sub-element of an R object 从 R 的列表中提取第一个、第二个、第三个……子元素并存储为列表 - Extract the first, second, third , … sub-element from a list in R and store as list Windows R CMD 检查顶级文件:(README)[警告]此文档格式需要非空<title>元素&lt;/div&gt;</title><div id="text_translate"><p> 我已在 Win、OSX 和 Linux 上为 R 激活 GitHub 对 CI 的操作。 在 Windows 上只发生以下警告(变成错误)。</p><pre> * checking top-level files... WARNING Conversion of 'README.md' failed: [WARNING] This document format requires a nonempty &lt;title&gt; element. Please specify either 'title' or 'pagetitle' in the metadata, eg by using --metadata pagetitle="..." on the command line. Falling back to 'README'</pre><p> <a href="https://github.com/UBESP-DCTV/covid19ita/pull/62/checks?check_run_id=594799232" rel="nofollow noreferrer">这里</a>是完整的报告。</p><p> 似乎它是由 CMD 直接检查调用的,因此我无法更改对 pandoc 的调用。</p><p> 另一方面,我尝试在README.Rmd header 中包含一个 pandoc 参数,正如<a href="https://community.rstudio.com/t/rmarkdown-document-format-requires-a-nonempty-title-element/36774" rel="nofollow noreferrer">这里</a>所建议的那样:</p><pre> output: github_document: pandoc_args: "--number-offset=1,0" toc: true pagetitle: covid19ita</pre><p> 它没有效果。</p><p> 注意:我不会从 CMD 检查中删除error_on = "warning" 。</p><p> 我该怎么做才能使测试也通过胜利?</p></div> - Windows R CMD check top-level files: (README) [WARNING] This document format requires a nonempty <title> element 在嵌套列表中查找元素的索引? - Find the indices of an element in a nested list? 指向嵌套列表元素的指针 - Pointer to a nested list element 删除 NULL 元素并从 R 中的嵌套列表中取消列出本地级别列表 - Remove NULL element and unlist the local level list from a nested list in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM