title: How do I use tidyr to fill in completed rows within each value of a grouping variable?
body: <p>Say I have data on people who choose between several options.  I have one row per person, and I want to have one row per person and choice option.  So, if I have 10 people who have 3 choices, right now I have 10 rows, and I want to have 30.</p>
  
  <p>All of the other variables should be copied to each of the new rows.  So, for example, if I have a variable for gender, that should be constant within ID.  (I am setting my data up this way to analyze with <code>mnlogit</code>.)</p>
  
  <p>This seems like the situation that two <code>tidyr</code> functions, <code>complete</code> and <code>fill</code>, were designed for.  To use a simple example:</p>
  
  <pre><code>library(lubridate)
  library(tidyr)
  dat &lt;- data.frame(
      id = 1:3,
      choice = 5:7,
      c = c(9, NA, 11),
      d = ymd(NA, "2015-09-30", "2015-09-29")
      )
  
  dat %&gt;% 
    complete(id, choice) %&gt;%
    fill(everything())
  
  # Source: local data frame [9 x 4]
  # 
  #      id choice     c          d
  #   (int)  (int) (dbl)     (time)
  # 1     1      5     9       &lt;NA&gt;
  # 2     1      6     9       &lt;NA&gt;
  # 3     1      7     9       &lt;NA&gt;
  # 4     2      5     9       &lt;NA&gt;
  # 5     2      6     9 2015-09-30
  # 6     2      7     9 2015-09-30
  # 7     3      5     9 2015-09-30
  # 8     3      6     9 2015-09-30
  # 9     3      7    11 2015-09-29
  </code></pre>
  
  <p>But this has some problems -- the values of d were carried forward correctly, but the values of c from ID 1 replaced the (correct) NA values for ID 2.</p>
  
  <p>I could try a workaround, like replacing all of the missing values with 999, running <code>complete</code> and <code>fill</code>, and then replacing 999 with NA.  (I think I would have to convert the date variables to character variables and then convert them back again if I go this route.)  But maybe someone on here knows of a tidy way to do this with <code>tidyr</code>?</p>
  
  <p>Edit: the desired output here is:</p>
  
  <pre><code># Source: local data frame [9 x 4]
  # 
  #     id     c          d choice
  #  (int) (dbl)     (time)  (int)
  # 1     1     9       &lt;NA&gt;      5
  # 2     1     9       &lt;NA&gt;      6
  # 3     1     9       &lt;NA&gt;      7
  # 4     2    NA 2015-09-30      5
  # 5     2    NA 2015-09-30      6
  # 6     2    NA 2015-09-30      7
  # 7     3    11 2015-09-29      5
  # 8     3    11 2015-09-29      6
  # 9     3    11 2015-09-29      7
  </code></pre>