title: Using a tibble to apply linear models by group to new data
body: <p>Let's say I have two datasets for the same group of irises over two years:</p>
  
  <pre><code># Create data for reproducible results.
  iris.2007 &lt;- iris
  iris.2008 &lt;- iris
  iris.2008[1:4] &lt;- 2*iris.2008[1:4]  # let's make the 2008 data different
  </code></pre>
  
  <p>I would like to fit a separate linear model for each species in the 2007 data, which I can do like this:</p>
  
  <pre><code># First nest by Species.
  iris.2007.nested &lt;- iris.2007 %&gt;%
                      group_by(Species) %&gt;%
                      nest()
  # Now apply the linear model call by group using the data.
  iris.2007.nested &lt;- iris.2007.nested %&gt;%
                      mutate(models = map(data,
                      ~ lm(Petal.Length ~ Petal.Width, data = .)))
  </code></pre>
  
  <p>When we look at the results, they make sense as a nicely-organized tibble.</p>
  
  <pre><code>head(iris.2007.nested)
  # A tibble: 3 × 3
       Species              data   models
        &lt;fctr&gt;            &lt;list&gt;   &lt;list&gt;
  1     setosa &lt;tibble [50 × 4]&gt; &lt;S3: lm&gt;
  2 versicolor &lt;tibble [50 × 4]&gt; &lt;S3: lm&gt;
  3  virginica &lt;tibble [50 × 4]&gt; &lt;S3: lm&gt;
  </code></pre>
  
  <p>Now let's do the same thing to the 2008 data.</p>
  
  <pre><code># First nest by species.
  iris.2008.nested &lt;- iris.2008 %&gt;%
                      group_by(Species) %&gt;%
                      nest()
  # Now apply the linear model call by species using the data.
  iris.2008.nested &lt;- iris.2008.nested %&gt;%
                      mutate(models = map(data,
                      ~ lm(Petal.Length ~ Petal.Width, data = .)))
  </code></pre>
  
  <p>Again, we end up with a nice tibble.</p>
  
  <pre><code>head(iris.2008.nested)
  # A tibble: 3 × 3
       Species              data   models
        &lt;fctr&gt;            &lt;list&gt;   &lt;list&gt;
  1     setosa &lt;tibble [50 × 4]&gt; &lt;S3: lm&gt;
  2 versicolor &lt;tibble [50 × 4]&gt; &lt;S3: lm&gt;
  3  virginica &lt;tibble [50 × 4]&gt; &lt;S3: lm&gt;
  </code></pre>
  
  <p>Now what I would like to do is use the linear models from the 2008 data to predict results using the 2007 data. Thinking that the best way to do that would be to combine the two datasets (retaining the group structure), here is what happens when I try to merge the two nested tibbles:</p>
  
  <pre><code>iris.both.nested &lt;- merge(iris.2007.nested, iris.2008.nested, by='Species')
  </code></pre>
  
  <p>As you can see below, the tibble no longer seems to have the same format as the individual tibbles above. Specifically, the organization is hard to discern (note that I am not including the full output in this chunk, but you get the idea).</p>
  
  <pre><code>head(iris.both.nested)
       Species
  1     setosa
  2 versicolor
  3  virginica
  
  data.x
  1 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, ...
  ... &lt;truncated&gt;
  1 1.327563, 0.5464903, -0.03686145, -0.03686145, -0.1368614, 0.06313855,
  ...
  </code></pre>
  
  <p>And although I can still apparently use the models fitted to the 2008 data (as models.y) to the data from 2007 (as data.x):</p>
  
  <pre><code>iris.both.nested.pred &lt;- iris.both.nested %&gt;%
                           mutate( pred = map2(models.y, 
                           data.x, predict))
  </code></pre>
  
  <p>The result is again not a nicely-organized tibble: (again not showing full output)</p>
  
  <pre><code>head(iris.both.nested.pred)
       Species
  1     setosa
  2 versicolor
  3  virginica
  
  data.x
  1 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, ...
  ... &lt;truncated&gt;
  1 1.327563, 0.5464903, -0.03686145, -0.03686145, -0.1368614,
  ...
  </code></pre>
  
  <p>So my question is -- is this process working even though the tibbles become strangely organized after the merge? Or am I missing something? Thanks!</p>