I am trying to make a big table by appending several small tables with the same number of columns using kableExtra. Specifically, suppose I have the following datasets and kables:
library(kableExtra)
df1 <- data.frame(x = c("a","b"), y=1:2)
df2 <- data.frame(x = c("c","d"), y=3:4)
k1 <- kable(df1, format = 'latex') |>
add_header_above(c("Header 1 " = 2))
k2 <- kable(df2, format = 'latex') |>
add_header_above(c("Header 2" = 2))
I would like to join k1 and k2 in the same table, obtaing as output:
\begin{tabular}{l|r}
\hline
\multicolumn{2}{c}{Header 1 } \\
\cline{1-2}
x & y\\
\hline
a & 1\\
\hline
b & 2\\
\hline
\multicolumn{2}{c}{Header 2} \\
\cline{1-2}
x & y\\
\hline
c & 3\\
\hline
d & 4\\
\hline
\end{tabular}
This would translate in:
However, I wouldn't like to join the data frames, because they come from several different aggregations. Can someone help me?
kableExtra
as it would involve merging columns, which does not seem to be a feature: stackoverflow.com/questions/58132626/…. You could get part of the way usingkableExtra::pack_rows
but this would fail to meet your "not to join data frames" criteria. I'm struggling to see how you could manage this without combining data frames as any table created programmatically would invariably include begin and end tabular so some intervention would be required to merge the two latex tabular environements.