3

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')
    )
  )
)
2
  • 1
    Are you looking for something like map_depth(testlist2, 3, "veggie") %>% unlist() in this case? Commented Nov 18, 2019 at 3:47
  • 1
    Or perhaps testlist2 %>% map(map_chr, map_chr, "veggie")? Commented Nov 18, 2019 at 3:49

2 Answers 2

2

As @MrFlick suggested, map_depth can help here

library(tidyverse)

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')
    )
  )
)

Use the first argument to set the desired list depth

testlist2 %>% map_depth(3, 'veggie')
#> [[1]]
#> [[1]][[1]]
#> [[1]][[1]][[1]]
#> [1] "apple"
#> 
#> 
#> [[1]][[2]]
#> [[1]][[2]][[1]]
#> [1] "banana"
#> 
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [[2]][[1]][[1]]
#> [1] "tomato"
#> 
#> 
#> [[2]][[2]]
#> [[2]][[2]][[1]]
#> [1] "pickle"
#> 
#> 
#> 
#> [[3]]
#> [[3]][[1]]
#> [[3]][[1]][[1]]
#> [1] "chestnut"
#> 
#> 
#> [[3]][[2]]
#> [[3]][[2]][[1]]
#> [1] "seaweed"

Or use negative numbers to count up from the lowest level

testlist2 %>% map_depth(-2, 'veggie')
#> [[1]]
#> [[1]][[1]]
#> [[1]][[1]][[1]]
#> [1] "apple"
#> 
#> 
#> [[1]][[2]]
#> [[1]][[2]][[1]]
#> [1] "banana"
#> 
#> 
#> 
#> [[2]]
#> [[2]][[1]]
#> [[2]][[1]][[1]]
#> [1] "tomato"
#> 
#> 
#> [[2]][[2]]
#> [[2]][[2]][[1]]
#> [1] "pickle"
#> 
#> 
#> 
#> [[3]]
#> [[3]][[1]]
#> [[3]][[1]][[1]]
#> [1] "chestnut"
#> 
#> 
#> [[3]][[2]]
#> [[3]][[2]][[1]]
#> [1] "seaweed"

Created on 2019-11-18 by the reprex package (v0.3.0)
Sign up to request clarification or add additional context in comments.

Comments

1

How about unlisting the data and select the elements with the required name ?

temp <- unlist(testlist2)
unname(temp[names(temp) == "veggie"])
#[1] "apple"    "banana"   "tomato"   "pickle"   "chestnut" "seaweed" 

This will work even when data is multiple layers deep.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.