打印数据框时如何更改stargazer中的列名?

Luc*_*aia 7 latex r stargazer

我正在尝试使用 stargazer 包在乳胶中输出数据帧。我希望列名包含乳胶代码,但 stargazer 不允许数据框名称中的乳胶代码。我也尝试使用 column.labels 参数,但此参数仅用于回归表,而不用于输出数据框。这是我尝试过的两种方法。都没有工作。

第一种方法 - 尝试更改数据框中变量的名称

代码

# 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$.")
Run Code Online (Sandbox Code Playgroud)

输出(显然观星者无法识别 LaTeX 代码):

% 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} 
Run Code Online (Sandbox Code Playgroud)

第二种方法——尝试使用 column.labels 参数

代码

# 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$.")
Run Code Online (Sandbox Code Playgroud)

输出(观星者只是忽略参数):

% 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} 
Run Code Online (Sandbox Code Playgroud)

Wei*_*ong 2

有点 hacky,但这里的主要思想是用我们想要的 Latex 代码gsub输出输出中的相关行:stargazer

# 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} 
Run Code Online (Sandbox Code Playgroud)

使用参数时align = T,尝试

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)
)
Run Code Online (Sandbox Code Playgroud)