title: Add any missing rows and columns by matching to a key of all possible rows and columns
body: <p>I am making tbls of the number of cases by year and age group for each county. Example df and vectors of all possible age groups, years, and counties:</p>
  
  <pre><code>    library(tidyverse)
  
      df &lt;- data.frame(
        "year" = c(2010, 2010, 2011, 2013, 2014, 2014, 
                   2015, 2015, 2015, 2016),
        "age_group" = c("20-39", "0-19", "0-19", "60-79", 
                        "80-99", "20-39","20-39", "60-79", 
                        "20-39", "80-99"),
        "county" = c("a", "b", "b", "a", "c", "b", "b", 
                     "a", "a", "a")) 
  
      all_age_groups &lt;- c("0-19", "20-39", "40-59", "60-79", "80-99")
  
      all_years &lt;- c(2010:2017)
  
      all_counties &lt;- c("a", "b", "c", "d")
  </code></pre>
  
  <p>Counties "a", "b", and "c" are missing SOME age groups or years. County "d" is missing ALL age groups and years (no cases). </p>
  
  <p>This works to add any missing age groups: </p>
  
  <pre><code>    ex_func &lt;- function(df_orig, selectcounty) {
        df_age_group &lt;- data.frame("age_group" = all_age_groups)
        df2 &lt;- df_orig %&gt;%
          filter(county == selectcounty) %&gt;%
          group_by(age_group, year) %&gt;%
          summarise(cases = n()) %&gt;%
          spread(year, cases) %&gt;%
          full_join(., df_age_group, by = "age_group") %&gt;%
          replace(is.na(.), 0) %&gt;%
          ungroup() %&gt;%
          mutate(age_group = factor(age_group, levels = all_age_groups)) %&gt;%
          arrange(age_group)
       df2
      }
  </code></pre>
  
  <p>But I haven't been able to successfully deal with the missing year columns without manually creating a new one for each missing year with <code>mutate</code>. </p>
  
  <p>I am looking for a way, preferably in <code>dplyr</code>, to:</p>
  
  <p>1) search for which years from <code>all_years</code> do not exist as columns in df</p>
  
  <p>2) add columns for each year that is missing</p>
  
  <p>3) make all rows for each new column equal to 0</p>
  
  <p>4) maintain the order of the years (2010-2017) in the columns, and age groups in df$age_group</p>
  
  <p>shorter version of desired outcome for county "b":</p>
  
  <pre><code>    df2 &lt;- ex_func(df, "b")
  
      age_group `2010` `2011 `2012` `2013` `2014 `2015`   
      0-19         1     1     0      0      0     0
      20-39        0     0     0      0      1     1
      40-59        0     0     0      0      0     0
      60-79        0     0     0      0      0     0
      80-99        0     0     0      0      0     0
  </code></pre>