Lucas De Abreu Maia
Lucas De Abreu Maia

Reputation: 597

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

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} 

Upvotes: 6

Views: 15915

Answers (2)

Seyma Kalay
Seyma Kalay

Reputation: 2863

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"))

Upvotes: 1

Weihuang Wong
Weihuang Wong

Reputation: 13128

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)
)

Upvotes: 2

Related Questions