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

2

6

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} 
Gape answered 30/10, 2016 at 4:5 Comment(0)
S
2

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)
)
Sheathe answered 30/10, 2016 at 4:51 Comment(2)
Excellent. But do you have any solution for when using the argument align = T? I tried putting the following within gsub(): past(names(vars), collapse = "} & \\multicolumn{1}{c}{"), but it doesn't work because, for some reason, paste outputs "\\" as two backslashes rather than just one (and it obviously doesn't accept just one backslash because it thinks I'm trying to scape the m in "\multicolumn").Gape
How to do this for HTML output stargazer(df, type = "html", summary = F, notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$."))?Clava
C
1

you can do something like this, but run the code before settings the name, you dont want to type an unrelevant name,

stargazer(GLM.1$finalModel, GLM.2$finalModel, GLM.3$finalModel,
      out = "my.GLMs.htm",
      title = "Logistic Models", type = "html", 
      single.row = TRUE, report = "vc*", df = FALSE, header = FALSE, digits=2, 
      column.labels = c("GLM 1", "GLM 2", "GLM 3"), apply.coef=exp, 
      p.auto=F, dep.var.caption  = "Odds Ratio", intercept.bottom = FALSE,
      covariate.labels=c( "Gender", "Marital Status","Age", "Employed",  "Education",
                          "Political Affiliation", "Rural", "Ethnicity", "Region", "Gross Income", "Networth","Networth-HomeEquity",  "Liquid Assets"))
Chur answered 3/11, 2019 at 18:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.