我想知道如何从嵌套列表的第n层中提取值。例如...如果这些都在第二层,我可以使用map()
来实现:
> testlist1 <-
+ list(
+ list('veggie' = 'apple', 'food' = 'bacon'),
+ list('veggie' = 'banana', 'food' = 'burger'),
+ list('veggie' = 'tomato', 'food' = 'sausage'),
+ list('veggie' = 'pickle', 'food' = 'chicken'),
+ list('veggie' = 'chestnut', 'food' = 'salmon'),
+ list('veggie' = 'seaweed', 'food' = 'tuna')
+ )
>
> testlist1 %>% map('veggie')
[[1]]
[1] "apple"
[[2]]
[1] "banana"
[[3]]
[1] "tomato"
[[4]]
[1] "pickle"
[[5]]
[1] "chestnut"
[[6]]
[1] "seaweed"
但是当它们更深时,我想必须有一些循环到图层的map()
方法吧?例如:
testlist2 <- list(
list(
list(
list('veggie' = 'apple', 'food' = 'bacon')
),
list(
list('veggie' = 'banana', 'food' = 'burger')
)
),
list(
list(
list('veggie' = 'tomato', 'food' = 'sausage')
),
list(
list('veggie' = 'pickle', 'food' = 'chicken')
)
),
list(
list(
list('veggie' = 'chestnut', 'food' = 'salmon')
),
list(
list('veggie' = 'seaweed', 'food' = 'tuna')
)
)
)
map_depth(testlist2, 3, "veggie") %>% unlist()
的东西? - undefinedtestlist2 %>% map(map_chr, map_chr, "veggie")
? - undefined