简体   繁体   English

为什么观星者以乳胶数学模式输出此表

[英]Why is stargazer outputting this table in latex maths mode

I have this data.frame which I'm trying to convert to latex code using stargazer: 我有这个data.frame,我正在尝试使用stargazer转换为乳胶代码:

habitats_df <- data.frame(habitat = c("beach", "grassland", "freshwater"), v1 = c(0.000, 0.670, 0.032), v2 = c(0.005, 0.824, 0.012))


 library(stargazer)
stargazer(habitats_df, summary = F)

 % Table created by stargazer v.4.5.3 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
    % Date and time: Wed, Jan 22, 2014 - 11:11:44
    \begin{table}[!htbp] \centering 
      \caption{} 
      \label{} 
    \begin{tabular}{@{\extracolsep{5pt}} ccc} 
    \\[-1.8ex]\hline 
    \hline \\[-1.8ex] 
    habitat & v1 & v2 \\ 
    \hline \\[-1.8ex] 
    beach & $0$ & $0.005$ \\ 
    grassland & $0.670$ & $0.824$ \\ 
    freshwater & $0.032$ & $0.012$ \\ 
    \hline \\[-1.8ex] 
    \normalsize 
    \end{tabular} 
    \end{table} 

Note stargazer is printing table in maths mode, therefore it enclosing numbers with $. 注意stargazer在数学模式下打印表格,因此它将数字括在$中。 How can I stop stargazer from printing table in latex maths mode? 如何在乳胶数学模式下从打印​​表中停止观星者?

Convert any numeric columns that you don't want as numeric to character: 将不需要的任何数字列转换为字符:

habitats_df$v1 <- as.character(habitats_df$v1)
> stargazer(habitats_df, summary = F)

% Table created by stargazer v.4.5.3 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Wed, Jan 22, 2014 - 11:23:59
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} ccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
habitat & v1 & v2 \\ 
\hline \\[-1.8ex] 
beach & 0 & $0.005$ \\ 
grassland & 0.67 & $0.824$ \\ 
freshwater & 0.032 & $0.012$ \\ 
\hline \\[-1.8ex] 
\normalsize 
\end{tabular} 
\end{table} 

As for the "why", it's because otherwise numbers aren't typeset correctly. 至于“为什么”,是因为否则数字的排版不正确。 If you have negative values, math mode will use a longer dash, and it will allow LaTeX to control the number of digits printed. 如果值为负数,则数学模式将使用更长的破折号,并允许LaTeX控制打印的位数。 Otherwise, as you can see above, if you want to exert control over the number of digits, you'll have to do that using sprintf in R. 否则,如您在上面看到的那样,如果您想控制位数,则必须在R中使用sprintf进行操作。

Hmisc:: latexSN does the job nicely. Hmisc :: latexSN做得很好。

habitats_df$v1 <- latexSN(habitats_df$v1)
habitats_df$v2 <- latexSN(habitats_df$v2)

library(stargazer)
stargazer(habitats_df, summary = F)

% Table created by stargazer v.4.5.3 by Marek Hlavac, Harvard University. E-mail: hlavac at fas.harvard.edu
% Date and time: Fri, Jan 24, 2014 - 08:32:39
\begin{table}[!htbp] \centering 
  \caption{} 
  \label{} 
\begin{tabular}{@{\extracolsep{5pt}} ccc} 
\\[-1.8ex]\hline 
\hline \\[-1.8ex] 
habitat & v1 & v2 \\ 
\hline \\[-1.8ex] 
beach & 0.000 & 0.005 \\ 
grassland & 0.670 & 0.824 \\ 
freshwater & 0.032 & 0.012 \\ 
\hline \\[-1.8ex] 
\normalsize 
\end{tabular} 
\end{table}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM