This question regards contingency tables in R as implemented with "table" or "xtab" and kableExtra (or with regard to any markdown/quarto output).
A minimal example is the following (which was created from a data frame using xtab):
df <- data.frame(matrix(c(-1,-1,0.41,1,-1,0.88,-1,1,0.32,1,1,0.82),byrow=T,ncol=3))names(df) <- c("A","B","y")tab <- xtabs(y ~ A + B, df)
Now R print output of course looks like this
BA -1 1 -1 0.41 0.32 1 0.88 0.82
Ouput of kableExtra::kable looks like this
<table><thead><tr><th style="text-align:left;"> </th><th style="text-align:right;"> -1 </th><th style="text-align:right;"> 1 </th></tr></thead><tbody><tr><td style="text-align:left;"> -1 </td><td style="text-align:right;"> 0.41 </td><td style="text-align:right;"> 0.32 </td></tr><tr><td style="text-align:left;"> 1 </td><td style="text-align:right;"> 0.88 </td><td style="text-align:right;"> 0.82 </td></tr></tbody></table>
which completely loses the names of the two dimensions.Imho some reasonable standard behaviour would be
<table><thead><tr><th style="border:none;text-align:center;"> </th><th style="border:none;text-align:center;"> </th><th style="border:none;text-align:center;" colspan="2">B</th></tr></thead><tbody><tr><td style="border:none;text-align:left;"> </td><td style="border:none;text-align:left;"> </td><td style="border:none;text-align:center;"> -1 </td><td style="border:none;text-align:center;"> 1 </td></tr><tr><th style="text-align:center;vertical-align:middle; width:4.2500rem; padding:0; border:none;" rowspan="2"> A </th><td style="border:none;text-align:left;"> -1 </td><td style="border:solid thin #000000;text-align:right;"> 0.41 </td><td style="border:solid thin #000000;text-align:right;"> 0.32 </td></tr><tr><td style="border:none;text-align:left;"> 1 </td><td style="border:solid thin #000000;text-align:right;"> 0.88 </td><td style="border:solid thin #000000;text-align:right;"> 0.82 </td></tr></tbody></table>
Am I doing something wrong? Would it be reasonable to implement this?