我正在尝试使自己熟悉purrr
,map
和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"
一种方法可能是:
map_chr(pluck(test_list, "outer_1"), pluck, "inner_1", "pluck")
[1] "String I Want"