【问题标题】:Using map to pluck from nested list使用 map 从嵌套列表中提取
【发布时间】:2020-03-18 16:09:47
【问题描述】:

我正在尝试熟悉 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 来抽象这个,但我错过了一些步骤。 (主要是我不知道如何使用map 模拟[[1]] 部分 - 类似于:

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

期望的输出

[1] "String I want"

【问题讨论】:

  • 您的列表深度为 4,但您只有三个级别,即outer_1inner_1pluck

标签: r purrr pluck


【解决方案1】:

一种方法可能是:

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

[1] "String I Want"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    • 1970-01-01
    • 2017-07-19
    • 1970-01-01
    相关资源
    最近更新 更多