使用地图从嵌套列表中抽取

问题描述 投票:1回答:1

我正在尝试使自己熟悉purrrmappluck,并且我有一个嵌套很深的列表:

test_list <- 
    list(
      outer_1 = list(
        list(
          inner_1 = list(pluck = "String I Want", dontpluck = "other string")
        )
      )
    )
$outer_1
$outer_1[[1]]
$outer_1[[1]]$inner_1
$outer_1[[1]]$inner_1$pluck
[1] "String I want"

$outer_1[[1]]$inner_1$dontpluck
[1] "other string"

而且我想提取"String I want"

我知道我可以使用该字符串

test_list$outer_1[[1]]$inner_1$pluck

但是我想使用map对此进行抽象,但是我缺少一些步骤。 (主要是我不知道如何使用[[1]]模拟map部分-类似于:

map(test_list, "outer_1") %>%
  map("inner_1") %>%
  map("pluck")

期望的输出

[1] "String I want"
r purrr pluck
1个回答
1
投票

一种方法可能是:

map_chr(pluck(test_list, "outer_1"), pluck, "inner_1", "pluck")

[1] "String I Want"
© www.soinside.com 2019 - 2024. All rights reserved.