1

我正在尝试熟悉purrrmap并且pluck我有一个嵌套很深的列表:

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

1 回答 1

1

一种方法可能是:

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

[1] "String I Want"
于 2020-03-18T16:22:38.920 回答