title: R: Merging two data frames where DF1 rownames = values in a column in DF2, AND where DF1 colnames = values from MULTIPLE columns in DF2
body: <p>I have an empty sample dataframe DF1 like this:</p>
  <pre><code>DF1 &lt;- matrix(data = NA, nrow = 4, ncol = 4)
  rownames(DF1) &lt;- c(&quot;Q1_2020&quot;, &quot;Q2_2020&quot;, &quot;Q3_2020&quot;, &quot;Q4_2020&quot;)
  colnames(DF1) &lt;- seq(1,4,1)
  DF1
           1  2  3  4
  Q1_2020 NA NA NA NA
  Q2_2020 NA NA NA NA
  Q3_2020 NA NA NA NA
  Q4_2020 NA NA NA NA
  </code></pre>
  <p>The colnames in DF1 represent a &quot;lag&quot; value which will also be included in DF2.</p>
  <p>I have another sample dataframe DF2 like this:</p>
  <pre><code>Loss &lt;- c(100,200,150,100,50,0,0,50,500,0)
  AccidentQtr &lt;- c(&quot;Q1_2020&quot;, &quot;Q1_2020&quot;, &quot;Q1_2020&quot;, &quot;Q2_2020&quot;, &quot;Q2_2020&quot;, &quot;Q2_2020&quot;, &quot;Q3_2020&quot;, &quot;Q3_2020&quot;, &quot;Q4_2020&quot;, &quot;Q4_2020&quot;)
  Q1_2020 &lt;- c(1,1,1,2,2,2,3,3,4,4)
  Q2_2020 &lt;- c(2,2,2,3,3,3,4,4,NA,NA)
  Q3_2020 &lt;- c(3,3,3,4,4,4,NA,NA,NA,NA)
  Q4_2020 &lt;- c(4,4,4,NA,NA,NA,NA,NA,NA,NA)
  DF2 &lt;- data.frame(Loss, AccidentQtr, Q1_2020, Q2_2020, Q3_2020, Q4_2020)
  DF2
  
     Loss AccidentQtr Q1_2020 Q2_2020 Q3_2020 Q4_2020
  1   100     Q1_2020       1       2       3       4
  2   200     Q1_2020       1       2       3       4
  3   150     Q1_2020       1       2       3       4
  4   100     Q2_2020       2       3       4      NA
  5    50     Q2_2020       2       3       4      NA
  6     0     Q2_2020       2       3       4      NA
  7     0     Q3_2020       3       4      NA      NA
  8    50     Q3_2020       3       4      NA      NA
  9   500     Q4_2020       4      NA      NA      NA
  10    0     Q4_2020       4      NA      NA      NA
  </code></pre>
  <p>The values in columns 3:6 are those same &quot;lag&quot; values from DF1 that I would like to join on.</p>
  <p>I want to aggregate the data from the &quot;Loss&quot; column in DF2 into the format of DF1 where the value in the AccidentQtr column = the row name in DF1, and  but am struggling with how I would do this or how I would even merge/join the data.</p>