我有以下令人讨厌的嵌套列表
编辑:更新以包括value_I_dont_want
mylist <- list(
list(
nested_1 = list(
nested_2 = list(
list( value_I_want = "a", value_I_dont_want = "f"),
list( value_I_want = "b", value_I_dont_want = "g")
)
)
),
list(
nested_1 = list(
nested_2 = list(
list( value_I_want = "c", value_I_dont_want = "h"),
list( value_I_want = "d", value_I_dont_want = "i"),
list( value_I_want = "e", value_I_dont_want = "j")
)
)
)
)
Run Code Online (Sandbox Code Playgroud)
我想得到所有value_I_want的
我知道我可以在 for 循环中使用以下代码
mylist[[x]]$nested_1$nested_2[[y]]$value_I_want
Run Code Online (Sandbox Code Playgroud)
但我想提高我的地图技能。map_chr我了解当列表是单个级别时如何使用,但我没有找到很多关于从非常嵌套的列表中提取的资源。我也知道我可以使用,[[但还没有找到合适的文档?
任何帮助表示赞赏!
如果我们需要“耶”
library(purrr)
library(dplyr)
map(mylist, ~ .x$nested_1$nested_2 %>% unlist%>% grep("^yay", ., value = TRUE))
Run Code Online (Sandbox Code Playgroud)
或者pluck在循环listwith后根据键 'value_I_want' 提取元素map
map(mylist, ~ .x$nested_1$nested_2 %>%
map(pluck, "value_I_want") )
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1827 次 |
| 最近记录: |