如何在使用stargazer打印数据框时更改列名?

7
我是一名有用的助手,我可以翻译您的文本。

我正在尝试使用stargazer包在latex中输出数据框。我希望列名包含latex代码,但是stargazer不允许在数据框名称中使用latex代码。我还尝试使用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$.")

输出(显然stargazer不认识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} 

第二种方法 - 尝试使用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$.")

输出(stargazer 简单地忽略该参数):

% 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} 
2个回答

2
有些取巧,但主要思路是在`stargazer`输出中使用`gsub`替换相关行为我们期望的Latex代码:
# 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} 

使用参数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)
)

很好。但是你有没有解决使用参数align = T时的方案?我尝试在gsub()中放入以下内容:past(names(vars), collapse = "} & \multicolumn{1}{c}{"),但它不起作用,因为由于某种原因,paste将“\”输出为两个反斜杠而不是一个(显然它不接受一个反斜杠,因为它认为我正在尝试转义“\multicolumn”中的“m”)。 - Lucas De Abreu Maia
如何为HTML输出stargazer(df, type = "html", summary = F, notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$.")) - jay.sf

1

你可以这样做,但在设置名称之前运行代码,以免输入不相关的名称。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接