如何使用purrr从嵌套列表中选择具有相同名称的元素?

Zsi*_*deZ 5 r purrr

require(purrr)

list <- list( 
  node = list(a = list(y = 1, t = 1), b = list(y = 1, t = 2)),
  node = list(a = list(y = 1, t = 3), b = list(y = 1, t = 4))) 
Run Code Online (Sandbox Code Playgroud)

如何用purrr选择所有"t"值?

aos*_*ith 5

您可以使用modify_depth如果您知道要从哪个级别提取信息,则

您想要提取t嵌套列表,即第 2 级。

modify_depth(list, 2, "t")

$node
$node$a
[1] 1

$node$b
[1] 2


$node
$node$a
[1] 3

$node$b
[1] 4
Run Code Online (Sandbox Code Playgroud)

purrr 系列modify函数返回与输入类型相同的对象,因此您需要unlist获取向量而不是列表。

  • 这是来自 *purrr* (2认同)