Variable group headers in stargazer pivot table

I want to automate the creation of descriptive tables with headers for groups of variables - using knitr and (ideally) stargazer. Since I need weighted descriptions, I do not use the stargazer created in the summary functions, but I create a data core containing statistics and use the argument summary=FALSEto print the data frame.

Problem 1: df with variables and headings in the form of strings, and the final statistics as columns do not work, because stargazer converts the NAheader lines to $$, which violates the knitting process.

Problem 2. As a work, I created a data frame with variables and headings in the form of columns and summary statistics in the form of rows and used the argument flip=TRUEso that the rows and columns are displayed at the output of stargazer. Although this allows me to have empty character vectors for headings and number vectors for variables, stargazer does not output number vectors in math mode, but (it seems) treats them as a symbol.

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 to add headers (empty lines) to the descriptive table and still get the math mode for variable statistics?

+4
source share
1 answer

, stargazer, , . xtable xtable:

\documentclass{article}
\usepackage{array}

\begin{document}

<<results = "asis", echo = FALSE>>=
library(xtable)

group1 <- data.frame(
  name = c("v1", "v2"),
  mean = 1:2, min = 3:4, max = 5:6,
  stringsAsFactors = FALSE)
group2 <- data.frame(
  name = c("v3", "v4"),
  mean = -(1:2), min = -(3:4), max = -(5:6),
  stringsAsFactors = FALSE)

dat <- rbind(
  c("\\textbf{Group 1}", rep(NA, ncol(group1) - 1)),
  group1,
  c("\\textbf{Group 2}", rep(NA, ncol(group1) - 1)),
  group2)

colnames(dat) <- sprintf("\\multicolumn{1}{c}{%s}", colnames(dat))

print.xtable(
  xtable(dat,
         caption = "Summary of Groups 1 and 2.",
         align = c("l", "l", rep(">{$}r<{$}", 3))),
  include.rownames = FALSE,
  sanitize.text.function = identity,
  sanitize.colnames.function = identity)
@
\end{document}

, , :

  • -, , , 2 2 3 .
  • rbind , , NA. LaTeX . ( \multicolumn, .)
  • 2 4 , , . "Width-1-multicolumn" , . .
  • align xtable, . . >{$}r<{$}, . here. ( l, - , .)
  • LaTeX , xtable sanitizer. sanitize.text.function sanitize.colnames.function identity.

:

Result

+2

Source: https://habr.com/ru/post/1651083/


All Articles