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.
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}
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}
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)
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With