如何使用purrr从嵌套列表中选择具有相同名称的元素? [英] How to select elements with the same name from nested list with purrr?

查看:51
本文介绍了如何使用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))) 

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

How to select all "t" values with purrr?

推荐答案

如果您知道要从哪个级别提取信息,您可以使用 modify_depth 来解决这个问题.

You can use modify_depth for this if you know what level you want to pull the information out of.

您想为嵌套列表提取 t,这是级别 2.

You want to pull t out for the nested lists, which is level 2.

modify_depth(list, 2, "t")

$node
$node$a
[1] 1

$node$b
[1] 2


$node
$node$a
[1] 3

$node$b
[1] 4

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

The purrr modify family of functions returns an object of the same type as the input, so you'd need to unlist to get a vector instead of a list.

这篇关于如何使用purrr从嵌套列表中选择具有相同名称的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆