【问题标题】:How to avoid changes in the row names when I use xtable package?使用 xtable 包时如何避免更改行名?
【发布时间】:2021-11-03 17:03:26
【问题描述】:

假设我有以下信息。

My_inf<-structure(c(0.00376, 0.00332, 0.00256, 0.00371, 0.00179, 0.00817, 
0.00817, 0.00746, 0.00959, 0.00533, 3.44713, 3.44713, 3.44713, 
3.44713, 3.44713, -3.44713, -3.44713, -3.44713, -3.44713, -3.44713, 
1.0005, 0.99994, 1.00172, 1.00108, 1.00147, -0.00941, -0.00833, 
-0.00816, -0.00956, -0.00468, 2.95108, 2.95429, 2.94188, 2.94577, 
2.94165), .Dim = c(5L, 7L), .Dimnames = list(c("NSW$^{+}$", "QLD$^{+}$", 
"SA$^{+}$", "TAS$^{+}$", "VIC$^{+}$"), c("Mean", "Median", "Max", 
"Min", "Std. Dev.", "Skewness", "Kurtosis")))

我想使用xtable 命令将此数据框转换为乳胶表。使用 xtable(My_inf) 但我得到以下输出。

\begin{table}[ht]
\centering
\begin{tabular}{rrrrrrrr}
 \hline
& Mean & Median & Max & Min & Std. Dev. & Skewness & Kurtosis \\ 
 \hline
NSW\$\verb|^|\{+\} \$& 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\ 
QLD\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\ 
 SA\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.94 \\ 
TAS\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\ 
VIC\$\verb|^|\{+\}\$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.00 & 2.94 \\ 
\hline
\end{tabular}
\end{table}

如您所见,行名称已根据xtable 命令更改。例如,我想要的输出是:

NSW$^{+}$ & 0.00 & 0.01 & 3.45 & -3.45 & 1.00 & -0.01 & 2.95 \\ 

我怎样才能得到我想要的输出?我应该更改原始数据框中的行名,或者为乳胶表使用另一个包。

【问题讨论】:

标签: r latex xtable


【解决方案1】:

这很有趣,但每个 R 包库都会生成一些其他代码。

观星者

library(stargazer)
stargazer(My_inf, type = 'latex')

% Table created by stargazer v.5.2.2 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Ср, ноя 03, 2021 - 20:42:52
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} cccccccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
 & Mean & Median & Max & Min & Std. Dev. & Skewness & Kurtosis \\ 
\hline \\[-1.8ex] 
NSW\$$\hat{\mkern6mu}$\{+\}\$ & $0.004$ & $0.008$ & $3.447$ & $$-$3.447$ & $1.000$ & $$-$0.009$ & $2.951$ \\ 
QLD\$$\hat{\mkern6mu}$\{+\}\$ & $0.003$ & $0.008$ & $3.447$ & $$-$3.447$ & $1.000$ & $$-$0.008$ & $2.954$ \\ 
SA\$$\hat{\mkern6mu}$\{+\}\$ & $0.003$ & $0.007$ & $3.447$ & $$-$3.447$ & $1.002$ & $$-$0.008$ & $2.942$ \\ 
TAS\$$\hat{\mkern6mu}$\{+\}\$ & $0.004$ & $0.010$ & $3.447$ & $$-$3.447$ & $1.001$ & $$-$0.010$ & $2.946$ \\ 
VIC\$$\hat{\mkern6mu}$\{+\}\$ & $0.002$ & $0.005$ & $3.447$ & $$-$3.447$ & $1.001$ & $$-$0.005$ & $2.942$ \\ 
\hline \\[-1.8ex] 
\end{tabular} 
\end{table} 

kableExtra

library(kableExtra)
kable(My_inf, "latex") 

\begin{tabular}{l|r|r|r|r|r|r|r}
\hline
  & Mean & Median & Max & Min & Std. Dev. & Skewness & Kurtosis\\
\hline
NSW\$\textasciicircum{}\{+\}\$ & 0.00376 & 0.00817 & 3.44713 & -3.44713 & 1.00050 & -0.00941 & 2.95108\\
\hline
QLD\$\textasciicircum{}\{+\}\$ & 0.00332 & 0.00817 & 3.44713 & -3.44713 & 0.99994 & -0.00833 & 2.95429\\
\hline
SA\$\textasciicircum{}\{+\}\$ & 0.00256 & 0.00746 & 3.44713 & -3.44713 & 1.00172 & -0.00816 & 2.94188\\
\hline
TAS\$\textasciicircum{}\{+\}\$ & 0.00371 & 0.00959 & 3.44713 & -3.44713 & 1.00108 & -0.00956 & 2.94577\\
\hline
VIC\$\textasciicircum{}\{+\}\$ & 0.00179 & 0.00533 & 3.44713 & -3.44713 & 1.00147 & -0.00468 & 2.94165\\
\hline
\end{tabular}

但如果您不满意 - 您可以解析输出的 LaTeX 代码并删除多余的符号。

【讨论】:

  • 请注意,您不应使用“stargazer”,因为‘stargazer’ is an utterly atrocious package。请改用“modelsummary”或“texreg”,或字面上的任何其他内容
  • @KonradRudolph 嗯,我不知道这个信息;)我会尝试用 texreg 制作一个乳胶代码,一会儿。
  • @KonradRudolph 一个补充。 Texreg 不适用于此数据:texreg(My_inf, single.row = TRUE) Error in extract(l[[i]], ...) : Neither texreg nor broom supports models of class matrix. 和modelsummary,此包使用kableExtra
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-12
  • 2017-07-07
  • 1970-01-01
  • 2017-01-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多