1

我有这个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 在数学模式下打印表格,因此它用 $ 括起来数字。如何阻止观星者在乳胶数学模式下打印表格?

4

2 回答 2

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 控制打印的位数。否则,如您在上面看到的,如果您想控制位数,则必须sprintf在 R中使用。

于 2014-01-22T18:27:12.650 回答
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}
于 2014-01-24T08:29:37.023 回答