I want to automate the generation of descriptive tables with headings for groups of variables - using knitr and (ideally) stargazer. Since I need weighted descriptives, I do not use stargazer's built in summary functions but generate a dataframe containing the statistics and use the summary=FALSE
argument to print the dataframe.
Issue 1: A df with the variables and headings as rows and the summary statistics as columns does not work because stargazer transforms the NA
s on the heading rows into $$
s which breaks the knitting process.
Issue 2: As a work around, I generated a dataframe with variables and headings as columns and the summary statistics as rows and use the flip=TRUE
argument to have rows and columns flipped in the stargazer output. While this allows me to have empty character vectors for the headings and numeric vectors for the variables, stargazer does not output the numeric vectors in math mode but (appears to) treat them as character.
Example:
# create example df
df <- data.frame(heading=c(" "," "," "),var1=c(1,2,3),var2=c(4,5,6))
df$heading <- as.character(df$heading)
# output using stargazer
stargazer(df, summary = FALSE, flip = TRUE)
% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Fri, Aug 12, 2016 - 10:39:01
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} cccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& 1 & 2 & 3 \\
\hline \\[-1.8ex]
heading & & & \\
var1 & 1 & 2 & 3 \\
var2 & 4 & 5 & 6 \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
Question: How do I add headings (empty rows) in the descriptive table and still get math mode output for the variable statistics?