Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change column names in stargazer when printing data frames?

Tags:

r

latex

stargazer

I'm trying to output a data frame in latex using the stargazer package. I want the column names to include latex code, but stargazer does not allow latex code inside data frame names. I've also tried to use the column.labels argument, but this argument is only used for regression tables, not for outputting data frames. Here's the two approaches I've tried. Neither worked.

First approach - trying to change the names of the variables in the data frame

Code:

# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)

# Changing names
names(df) = c("$X$", "$Y$\\textsuperscript{1}")

# Exporting
stargazer(df, summary = F, 
  notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")

Output (clearly stargazer doesn't recognize the LaTeX code):

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:46:22
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} ccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & \$X\$ & \$Y\$\textbackslash textsuperscript\{1\} \\ 
\hline \\[-1.8ex] 
1 & $1$ & $6$ \\ 
2 & $2$ & $7$ \\ 
3 & $3$ & $8$ \\ 
4 & $4$ & $9$ \\ 
5 & $5$ & $10$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

Second approach – trying to use the column.labels argument

Code:

# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)

# Exporting
stargazer(df, summary = F, 
  column.labels = c("$X$", "$Y$\\textsuperscript{1}"),
  notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")

Output (stargazer simply ignores the argument):

% Table created by stargazer v.5.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Sat, Oct 29, 2016 - 20:57:41
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} ccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & x & y \\ 
\hline \\[-1.8ex] 
1 & $1$ & $6$ \\ 
2 & $2$ & $7$ \\ 
3 & $3$ & $8$ \\ 
4 & $4$ & $9$ \\ 
5 & $5$ & $10$ \\ 
\hline \\[-1.8ex] 
\multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \\ 
\end{tabular} 
\end{table} 
like image 620
Lucas De Abreu Maia Avatar asked Oct 30 '16 04:10

Lucas De Abreu Maia


1 Answers

A bit hacky, but the main idea here is to gsub out the relevant row in the stargazer output with our desired Latex code:

# Creating a data frame
df = data.frame(x = 1:5, y = 6:10)

out <- capture.output(
  stargazer(df, summary = F, 
    notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")
)

# Changing names
vars = c("x" = "$X$", "y" = "$Y$\\\\textsuperscript{1}")
cat(sep = "\n",
  gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
# \begin{table}[!htbp] \centering 
#   \caption{} 
#   \label{} 
# \begin{tabular}{@{\extracolsep{5pt}} ccc} 
# \\[-1.8ex]\hline 
# \hline \\[-1.8ex] 
#  & $X$ & $Y$\textsuperscript{1} \\ 
# \hline \\[-1.8ex] 
# 1 & $1$ & $6$ \\ 
# 2 & $2$ & $7$ \\ 
# 3 & $3$ & $8$ \\ 
# 4 & $4$ & $9$ \\ 
# 5 & $5$ & $10$ \\ 
# \hline \\[-1.8ex] 
# \multicolumn{3}{l}{\textsuperscript{1} This is a note that was supposed to refer to $Y$.} \\ 
# \end{tabular} 
# \end{table} 

When using argument align = T, try

vars = sprintf("\\\\multicolumn\\{1\\}\\{c\\}\\{%s\\}", 
  c("$X$", "$Y$\\\\textsuperscript{1}"))
names(vars) <- sprintf("\\\\multicolumn\\{1\\}\\{c\\}\\{%s\\}", names(df))
cat(sep = "\n",
  gsub(paste(names(vars), collapse = " & "), paste(vars, collapse = " & "), out)
)
like image 68
Weihuang Wong Avatar answered Sep 29 '22 16:09

Weihuang Wong