title: Use filter() (and other dplyr functions) inside nested data frames with map()
body: <p>I'm trying to use <code>map()</code> of <code>purrr</code> package to apply <code>filter()</code> function to the data stored in a nested data frame.</p>
  
  <p><em>"Why wouldn't you filter first, and then nest?</em> - you might ask.
  That will work (and I'll show my desired outcome using such process), but I'm looking for ways to do it with <code>purrr</code>.
  I want to have just one data frame, with two list-columns, both being nested data frames - one full and one filtered.</p>
  
  <p>I can achieve it now by performing <code>nest()</code> twice: once on all data, and second on filtered data:</p>
  
  <pre><code>library(tidyverse)
  
  df &lt;- tibble(
    a = sample(x = rep(c('x','y'),5), size = 10),
    b = sample(c(1:10)),
    c = sample(c(91:100))
  )
  
  df_full_nested &lt;- df %&gt;% 
    group_by(a) %&gt;% 
    nest(.key = 'full')
  
  df_filter_nested &lt;- df %&gt;%
    filter(c &gt;= 95) %&gt;%  ##this is the key step
    group_by(a) %&gt;% 
    nest(.key = 'filtered')
  
  ## Desired outcome - one data frame with 2 nested list-columns: one full and one filtered.
  ## How to achieve this without breaking it out into 2 separate data frames?
  df_nested &lt;- df_full_nested %&gt;% 
    left_join(df_filter_nested, by = 'a')
  </code></pre>
  
  <p>The objects look like this:</p>
  
  <pre><code>&gt; df
  # A tibble: 10 x 3
         a     b     c
     &lt;chr&gt; &lt;int&gt; &lt;int&gt;
   1     y     8    93
   2     x     9    94
   3     y    10    99
   4     x     5    97
   5     y     2   100
   6     y     3    95
   7     x     7    96
   8     y     6    92
   9     x     4    91
  10     x     1    98
  
  &gt; df_full_nested
  # A tibble: 2 x 2
        a             full
    &lt;chr&gt;           &lt;list&gt;
  1     y &lt;tibble [5 x 2]&gt;
  2     x &lt;tibble [5 x 2]&gt;
  
  &gt; df_filter_nested
  # A tibble: 2 x 2
        a         filtered
    &lt;chr&gt;           &lt;list&gt;
  1     y &lt;tibble [3 x 2]&gt;
  2     x &lt;tibble [3 x 2]&gt;
  
  &gt; df_nested
  # A tibble: 2 x 3
        a             full         filtered
    &lt;chr&gt;           &lt;list&gt;           &lt;list&gt;
  1     y &lt;tibble [5 x 2]&gt; &lt;tibble [4 x 2]&gt;
  2     x &lt;tibble [5 x 2]&gt; &lt;tibble [4 x 2]&gt;
  </code></pre>
  
  <p>So, this works. But it is not clean. And in real life, I group by several columns, which means I also have to join on several columns... It gets hairy fast.</p>
  
  <p>I'm wondering if there is a way to apply filter to the nested column. This way, I'd operate within the same object. Just cleaner and more understandable code.</p>
  
  <p>I'm thinking it'd look like </p>
  
  <pre><code>df_full_nested %&gt;% mutate(filtered = map(full, ...))
  </code></pre>
  
  <p>But I am not sure how to map <code>filter()</code> properly</p>
  
  <p>Thanks!</p>