I try to build a data summary table using the following format.
But outcome is quite not pretty, for example, \$\textbackslash pm\$
, which is supposed to be $\pm$
instead. I can hardly find any problem in my codes. Any suggestion, please?
library(stargazer)
library(qwraps2)
data("mtcars2")
our_summary1 <-
list("Miles Per Gallon" =
list("min" = ~ min(mpg),
"max" = ~ max(mpg),
"mean (sd)" = ~ qwraps2::mean_sd(mpg)),
"Displacement" =
list("min" = ~ min(disp),
"median" = ~ median(disp),
"max" = ~ max(disp),
"mean (sd)" = ~ qwraps2::mean_sd(disp)),
"Weight (1000 lbs)" =
list("min" = ~ min(wt),
"max" = ~ max(wt),
"mean (sd)" = ~ qwraps2::mean_sd(wt)),
"Forward Gears" =
list("Three" = ~ qwraps2::n_perc0(gear == 3),
"Four" = ~ qwraps2::n_perc0(gear == 4),
"Five" = ~ qwraps2::n_perc0(gear == 5))
)
by_cyl2 <- summary_table(mtcars2, summaries = our_summary1, by = c("cyl_factor"))
stargazer(by_cyl2)
Result:
\begin{table}[!htbp] \centering
\caption{}
\label{}
\begin{tabular}{@{\extracolsep{5pt}} cccc}
\\[-1.8ex]\hline
\hline \\[-1.8ex]
& 6 cylinders (N = 7) & 4 cylinders (N = 11) & 8 cylinders (N = 14) \\
\hline \\[-1.8ex]
min & 17.8 & 21.4 & 10.4 \\
max & 21.4 & 33.9 & 19.2 \\
mean..sd. & 19.74 \$\textbackslash pm\$ 1.45 & 26.66 \$\textbackslash pm\$ 4.51 & 15.10 \$\textbackslash pm\$ 2.56 \\
min.1 & 145 & 71.1 & 275.8 \\
median & 167.6 & 108 & 350.5 \\
max.1 & 258 & 146.7 & 472 \\
mean..sd..1 & 183.31 \$\textbackslash pm\$ 41.56 & 105.14 \$\textbackslash pm\$ 26.87 & 353.10 \$\textbackslash pm\$ 67.77 \\
min.2 & 2.62 & 1.513 & 3.17 \\
max.2 & 3.46 & 3.19 & 5.424 \\
mean..sd..2 & 3.12 \$\textbackslash pm\$ 0.36 & 2.29 \$\textbackslash pm\$ 0.57 & 4.00 \$\textbackslash pm\$ 0.76 \\
Three & 2 (29) & 1 (9) & 12 (86) \\
Four & 4 (57) & 8 (73) & 0 (0) \\
Five & 1 (14) & 2 (18) & 2 (14) \\
\hline \\[-1.8ex]
\end{tabular}
\end{table}
CodePudding user response:
As far as I can see, stargazer()
provides no way to allow LaTeX code in the labels in your table. What you could do instead is to replace the latex $\pm$
that was put there by qwraps2
with something that stargazer
will leave alone, run stargazer
, then put back the LaTeX code. For example,
hidepm <- function(s, replacement)
gsub("\\$\\\\pm\\$", replacement, s)
restorepm <- function(s, replacement)
gsub(replacement, "$\\\\pm$", s)
stargazer2 <- function(x, replacement = "PlusMinus", ...)
restorepm(stargazer(hidepm(x, replacement = replacement), ...),
replacement = replacement)
stargazer2(by_cyl2)
If you happen to have the string "PlusMinus" in your table (or it causes some other problem), use a different replacement.
CodePudding user response:
I get this output from qwraps2::mean_sd()
function. Latex ignores the forward-slash which is likely the issue. Perhaps use another library to calculate standard-deviation?
"15.40 $\pm$ 5.29"
CodePudding user response:
Other packages generate a relatively same output:
Xtable:
library(xtable)
xtable(by_cyl2)
F.e.:
mean..sd..1 & 183.31 \$$\backslash$pm\$ 41.56 & 105.14 \$$\backslash$pm\$ 26.87 & 353.10 \$$\backslash$pm\$ 67.77 \\
kableExtra:
library(kableExtra)
kable(by_cyl2, "latex")
F.e.:
mean (sd) & 183.31 \$\textbackslash{}pm\$ 41.56 & 105.14 \$\textbackslash{}pm\$ 26.87 & 353.10 \$\textbackslash{}pm\$ 67.77\\
If you aren't satisfied with this output - you can manually parse and change \$\textbackslash pm\$
to $\pm$
.