I'm trying to see how to extract values from the nth level of a nested list. For example...if these were all on the second layer, I could just use map() like so:
> 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"
But when they are deeper, I imagine there must be some map() method of looping to the layers? Example:
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()in this case?testlist2 %>% map(map_chr, map_chr, "veggie")?