Eve Chanatasig
Eve Chanatasig

Reputation: 397

How to avoid changes in the row names when I use xtable package?

Let's say that I have the following information.

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

I want to use the xtable command for transform this data frame into a latex table. Using xtable(My_inf) but i get the following output.

\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}

As you can see the row names have changed according to xtable command. My desired output would be, for example:

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

How can I get my desired output? I should change the row names in the original data frame or maybe use another package for latex tables.

Upvotes: 0

Views: 108

Answers (1)

manro
manro

Reputation: 3677

This is interesting, but every R package library generates a little another code.

Stargazer

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}

But if you are discontented - you can parse an output LaTeX code and remove redundant symbols.

Upvotes: 1

Related Questions