为什么观星者在乳胶数学模式下输出这个表格

时间:2014-01-22 11:07:34

标签: r 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} 

注意stargazer是数学模式下的打印表,因此它用$括起数字。如何在乳胶数学模式下阻止观星者进行打印?

2 个答案:

答案 0 :(得分:1)

将您不想要的任何数字列转换为字符:

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} 

至于“为什么”,这是因为否则数字没有正确排版。如果您有负值,数学模式将使用更长的破折号,它将允许LaTeX控制打印的位数。否则,正如您在上面所看到的,如果您想对数字位数进行控制,则必须使用R中的sprintf来执行此操作。

答案 1 :(得分:0)

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}