title: accessing nested lists in R
body: <p>I have created a double nested structure for some data. How can I Access the data on the 2nd Level ( or for that matter the nth Level?)</p>
  
  <pre><code>library(gapminder)
  library(purrr)
  library(tidyr)
  gapminder
  nest_data &lt;- gapminder %&gt;% group_by(continent) %&gt;% nest(.key = by_continent) 
  
  nest_2&lt;-nest_data %&gt;% mutate(by_continent = map(by_continent, ~.x %&gt;% group_by(country) %&gt;% nest(.key = by_country)))
  </code></pre>
  
  <p>How can I now get the data for China into a dataframe or tibble from nest_2?</p>
  
  <p>I can get the data for all of Asia, but I'm unable to isolate China.</p>
  
  <pre><code>a&lt;-nest_2[nest_2$continent=="Asia",]$by_continent  ##Any better way of isolating Asia from nest_2?
  </code></pre>
  
  <p>I thought I could then do</p>
  
  <pre><code>b&lt;-a[a$country=="China",]$by_country 
  </code></pre>
  
  <p>But I get the following error </p>
  
  <pre><code>Error in a[a$country == "China", ] : incorrect number of dimensions 
  
  
  
  &gt; glimpse(a)
  List of 1
   $ :Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   33 obs. of  2 variables:
    ..$ country   : Factor w/ 142 levels "Afghanistan",..: 1 8 9 19 25 56 59 60 61 62 ...
    ..$ by_country:List of 33
  </code></pre>
  
  <p>So my big error was not recognizing that the product was a list, which could be remedied by adding [[1]] in the end. However, I very much liked the solution by @Floo0. I took the liberty of providing a function taking the names of the variables in case the sequence of columns are different from the one provided. </p>
  
  <pre><code>select_unnest &lt;- function(df, listcol, var, var_val){  ###listcol, var and var_val must enclosed by ""
    df[[listcol]][df[[var]]==var_val][[1]]
  }
  
  nest_2 %&gt;% select_unnest(listcol = "by_continent", var = "continent", var_val = "Asia") %&gt;% 
    select_unnest(listcol = "by_country", var = "country", var_val = "China")
  </code></pre>