【问题标题】:How to change column names in stargazer when printing data frames?打印数据框时如何更改观星器中的列名?
【发布时间】:2016-10-30 04:05:38
【问题描述】:

我正在尝试使用 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$.")

输出(显然 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} 

【问题讨论】:

    标签: r latex stargazer


    【解决方案1】:

    有点老套,但这里的主要思想是使用我们所需的 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} 
    

    当使用参数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}{"),但它不起作用,因为出于某种原因,粘贴输出“ \\" 作为两个反斜杠,而不仅仅是一个(它显然不接受一个反斜杠,因为它认为我正在尝试对 "\multicolumn" 中的 m 进行转义)。
    • HTML输出stargazer(df, type = "html", summary = F, notes = "\\textsuperscript{1} This is a note that was supposed to refer to $Y$."))如何做到这一点?
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-13
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      相关资源
      最近更新 更多