title: How do I set header labels in a flextable with multiple header rows in R?
body: <p>I am using the flextable-function in R to create a nice looking flat contingency table. My flat contingency table has column headers in two rows. I tried to change these using the <code>set_header_labels</code>-function in the flextable-package but failed. </p>
  
  <p>The steps below represent the process that I went through. The first five steps went fine. I failed to arrive at my desired result in the final step where I want to change column header names from "A" to "aa" and from "B" to "bb". Please be advised that changing "A" to "aa" etc. at the very start is not solving my problem. I really need to make the changes I want, at the very end, not at the beginning! </p>
  
  <ol>
  <li>Create a tibble. </li>
  </ol>
  
  <pre><code>t &lt;- tibble(ID = 1:10, 
  header1 = c("A", "A", "A", "A", "A", "B", "B", "B", "B", "B"),
  header2 = c("No", "No", "No", "Yes", "Yes", "Yes", "Yes", "No", "Yes", "No"),
  q1 = c(1, 3, 2, 2, 3, 1, 2, 1, 1, 3))
  </code></pre>
  
  <ol start="2">
  <li>Create a flat contingency table. </li>
  </ol>
  
  <p><code>ft &lt;- (t %&gt;% ftable(row.vars = c("q1"), col.vars = c("header1", "header2")))</code></p>
  
  <ol start="3">
  <li>Load the function to convert a flat contingency table to a flextable. Source: <a href="https://stackoverflow.com/questions/52661289/is-there-an-easy-way-to-convert-flat-contingency-tables-ftable-to-flextable">Is there an (easy) way to convert flat contingency tables (ftable) to flextable</a></li>
  </ol>
  
  <pre><code>ftable_to_flextable &lt;- function( x ){
  
    row.vars = attr( x, "row.vars" )
    col.vars = attr( x, "col.vars" )
    rows &lt;- rev(expand.grid( rev(row.vars), stringsAsFactors = FALSE ))
    cols &lt;- rev(expand.grid( rev(col.vars), stringsAsFactors = FALSE ))
  
    xmat &lt;- as.matrix(x)
    cols$col_keys = dimnames(xmat)[[2]]
    xdata &lt;- cbind(
      data.frame(rows, stringsAsFactors = FALSE),
      data.frame(xmat, stringsAsFactors = FALSE)
    )
    names(xdata) &lt;- c(names(row.vars), cols$col_keys)
  
    ft &lt;- regulartable(xdata)
    ft &lt;- set_header_df(ft, cols)
    ft &lt;- theme_booktabs(ft)
    ft &lt;- merge_v(ft, j = names(row.vars))
    ft
  }
  </code></pre>
  
  <ol start="4">
  <li>Create a flextable.</li>
  </ol>
  
  <p><code>flext &lt;- ftable_to_flextable(ft)</code></p>
  
  <ol start="5">
  <li>Merge two header titles.</li>
  </ol>
  
  <pre><code>flext &lt;- merge_at(flext, i = 1, j = 2:3, part = "header")
  flext &lt;- merge_at(flext, i = 1, j = 4:5, part = "header")
  </code></pre>
  
  <p>Running <code>flext</code> now returns:</p>
  
  <p><a href="https://i.stack.imgur.com/JFOYl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JFOYl.png" alt="enter image description here"></a></p>
  
  <ol start="6">
  <li>Now, I want the change the two labels in the first header row of the flat contingency table. </li>
  </ol>
  
  <p><code>set_header_labels(flext, A = "aa", B = "bb")</code></p>
  
  <p>I get the following error message:</p>
  
  <pre><code>Error in `[&lt;-`(`*tmp*`, i, j, value = value) : subscript out of bounds
  </code></pre>