Reputation: 1561
I'm creating APA style tables using r markdown, latex and the package papaja with the function apa_table()
for pdf documents. I would like to add an additional named col_spanner on top of the existing ones (two, with names 'Cars 1' and 'Cars 2', see picture), spanning from column 2 up to and including column 9 with the name or header 'Cars'.
With kableExtra() this is not a problem, you would just add: add_header_above(c(" ", "Cars" = 8))
. But this doesn't work with papaja, latex and pdf files (as far as i could figure out).
Code and picture included to illustrate.
{r , results= 'asis'}
library(papaja)
my_table <- t(apply(cars, 2, function(x) {
round(c(Mean = mean(x), SD = sd(x), Min = min(x), Max = max(x)), 2)
}))
apa_table(cbind(my_table, my_table),
align = c("l", rep("r", 8)),
caption = "A summary table of the cars dataset.",
note = "This table was created using apa\\_table()",
added_stub_head = "Variables",
col_spanners = list(`Cars 1` = c(2, 5), `Cars 2` = c(6, 9)))
Upvotes: 2
Views: 1334
Reputation: 23
This is a workaround, not a complete solution, but you can write the table code in LaTex and insert this as a chunk into the markdown script.
If you knit the document with the APA_table to PDF, you also output the .tex file. Open this and find the code relevant to your table, you can then bookend your table with the APA formatted code.
For example the APA_table relevant prefix code would be:
\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\caption{\label{tab:transformtable}Caption goes here} %The label allows for referencing the 'chunk'/table
\begin{tabular}{llllll}
Enter your LaTex table code and contents here, using \multicolumn{}{}{} for spanning columns and bookend with:
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}
My final complete code was
\begin{table}[tbp]
\begin{center}
\begin{threeparttable}
\caption{\label{tab:transformtable}}
\begin{tabular}{llllll}
\toprule
& &\multicolumn{2}{c}{Shapiro-Wilks} &\multicolumn{2}{c}{\emph{p}-value} \\
\cmidrule{3-6}
Measure & Transform Power & \multicolumn{1}{c}{Original} & \multicolumn{1}{c}{Updated} & \multicolumn{1}{c}{Original} & \multicolumn{1}{c}{Updated}\\
\midrule
OVT & 1.425 & .96 & .98 & .006 & .036\\
PURL & .025 & .70 & .99 & <.001 & .008\\
\bottomrule
\end{tabular}
\end{threeparttable}
\end{center}
\end{table}
Inserted as a LaTex chunk "```{=latex}" then knits the APA formatted table into the PDF.
Upvotes: 0