title: Add rows to a data set based on 2 grouping vectors
body: <p>I have a data set that looks like the picture below. For everyday NJ (person) and every stimulus/ProductName) I want to have 10 listed attributes. Currently only attributes that are used are listed (so a different number of attributes for different stimuli) but I want all 10 listed each time with 0 across the row if unused. Anyone know how I can add these rows for every NJ and stimulus?<a href="https://i.stack.imgur.com/aFyrQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aFyrQ.png" alt="enter image description here" /></a></p>
  <p>The new code:</p>
  <pre><code>stimulus &lt;- 1:16
  factor_attribute   &lt;- paste0(&quot;factor_attribute&quot;,1:10)
                               
  #&quot;Beadiness&quot;, &quot;Chewiness&quot;, &quot;Contrast&quot;, &quot;Crumbliness&quot;, &quot;Grittiness&quot;,&quot;Hardness&quot;,
   #&quot;Lumpiness&quot;, &quot;Roughness&quot;, &quot;Smoothness&quot;, &quot;Softness&quot;)
  
  all &lt;- expand.grid(NJ = NJ, stimulus = stimulus, factor_attribute = factor_attribute)
  all$x30 &lt;- 0
  all$x29 &lt;- 0
  all$x28 &lt;-0
  all$x27 &lt;-0
  all$x26 &lt;-0
  all$x25 &lt;-0
  all$x24 &lt;-0
  all$x23 &lt;-0
  all$x22 &lt;-0
  all$x21 &lt;-0
  all$x20 &lt;-0
  all$x19 &lt;-0
  all$x18 &lt;-0
  all$x17 &lt;-0
  all$x16 &lt;-0
  all$x15 &lt;-0
  all$x14 &lt;-0
  all$x13 &lt;-0
  all$x12 &lt;-0
  all$x11 &lt;-0
  all$x10 &lt;-0
  all$x9 &lt;-0
  all$x8 &lt;-0
  all$x7 &lt;-0
  all$x6 &lt;-0
  all$x5 &lt;-0
  all$x4 &lt;-0
  all$x3 &lt;-0
  all$x2 &lt;-0
  all$x1 &lt;-0
  all$x0 &lt;-0
   
  glimpse(all)
  
  tcata3 &lt;- full_join(all, tcata2.df, 
            by = c(&quot;NJ&quot;, &quot;stimulus&quot;, &quot;factor_attribute&quot;), 
            suffix = c(&quot;&quot;,&quot;&quot;)) %&gt;% 
    replace(is.na(.),0)```
  
  &gt; head(tcata3)
    ```NJ stimulus  factor_attribute x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27
  1  1        1 factor_attribute1  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  2  2        1 factor_attribute1  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  3  3        1 factor_attribute1  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  4  4        1 factor_attribute1  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  5  5        1 factor_attribute1  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  6  6        1 factor_attribute1  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
    x28 x29 x30 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30
  1   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  2   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  3   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  4   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  5   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  6   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0```
   tail(tcata3)
       ```NJ stimulus factor_attribute x0 x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20 x21 x22 x23 x24 x25 x26 x27
  9265 39       16         Contrast  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  9266 39       16      Crumbliness  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  9267 39       16       Grittiness  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  9268 39       16         Hardness  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  9269 39       16        Roughness  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  9270 39       16       Smoothness  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
       x28 x29 x30 X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30
  9265   0   0   0  0  0  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
  9266   0   0   0  0  0  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
  9267   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   1   1   1   1   1   1   1   1   1   1   1   1   1   1
  9268   0   0   0  0  0  0  0  0  0  0  0  0  0   0   0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
  9269   0   0   0  0  0  0  0  0  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
  9270   0   0   0  1  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1```
  ```&gt; head(tcata2.df)
    NJ stimulus X0 X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 X16 X17 X18 X19 X20 X21 X22 X23 X24 X25 X26 X27 X28 X29 X30
  1  1        1  0  0  0  1  1  1  1  1  1  1   1   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
  2  1        1  0  0  0  0  0  0  0  0  1  1   1   1   1   1   1   1   1   1   0   0   0   0   0   0   0   0   0   0   0   0   0
  3  1        1  0  1  1  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
  4  1        1  0  0  0  0  0  0  0  0  0  0   0   0   0   0   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1
  5  1        2  0  0  0  1  1  1  1  1  1  1   1   1   1   1   1   1   1   1   1   1   1   0   0   0   0   0   0   0   0   0   0
  6  1        2  0  0  0  0  0  0  0  0  0  0   0   0   0   0   0   0   0   0   0   0   1   1   1   1   1   1   1   1   1   0   0
    factor_attribute
  1        Chewiness
  2        Lumpiness
  3       Smoothness
  4         Softness
  5      Crumbliness
  6       Grittiness```
  Now I have done the following ```tcata3 &lt;- full_join(all, tcata2.1.df, 
            by = c(&quot;NJ&quot;, &quot;stimulus&quot;, &quot;factor_attribute&quot;), 
            suffix = c(&quot;&quot;,&quot;&quot;)) %&gt;% 
    replace(is.na(.),0)
  
  head(tcata3)
  
  tcata3[tcata3$factor_attribute==&quot;factor_attribute1&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Chewiness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute2&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Lumpiness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute3&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Smoothness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute4&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Softness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute5&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Crumbliness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute6&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Grittiness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute7&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Beadiness&quot;
  
  tcata3[tcata3$factor_attribute==&quot;factor_attribute8&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Roughness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute9&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Hardness&quot;
  tcata3[tcata3$factor_attribute==&quot;factor_attribute10&quot;, &quot;factor_attribute&quot;] &lt;- &quot;Contrast``` but the attributes don't merge and I end up with vectors ```x0:x30``` twice.
  </code></pre>