有效地在 R 中呈现非常小的数字

Presenting very small numbers in R effectively

想象一下 table,其中每一行都是一个不同的变量,列是系数和 P 值:

           Coef       P-Value
 [1,] -0.0005017369 1.787221e-03
 [2,] -0.0096543344 0.000000e+00

当我使用 xtable 插入乳胶 xtable(table,digits=3) 我得到这个:

\begin{table}[ht]
\centering
\begin{tabular}{rrr}
  \hline
 & 1 & 2 \ 
  \hline
1 & -0.001 & 0.002 \ %you can see it rounds the coefs and I don't want that
  2 & -0.010 & 0.000 \ 
   \hline
\end{tabular}
\end{table}

然而有些系数非常小:例如 0.000001。 我想要做的是呈现这样的结果:

           Coef       
 [1,] -5.01 e^-4 (***)
 [2,] -9.65 e^-3 (***)

With (***) when P-Value < 0.01 
      (**) when 0.01 < P-V < 0.05 and
       (*) when 0.05 < P-V < 0.1

以下是示例的一些数据:

coeffs<-c(-0.0813492327 ,-0.0096543344 ,-1.0854510145 , 0.0114780266 ,-0.0018292747, -0.1953482934 ,-0.0370855156,  0.0643568136,
-0.1113654966, -0.0043410195,  0.0092573323, -0.0001360552, -0.0001318953, -0.0001374126, -0.0005017369) ,
pv<-c(1.955660e-02, 0.000000e+00, 0.000000e+00, 7.960061e-01, 7.435434e-01, 6.699631e-06, 6.260328e-01, 1.012333e-01, 4.201014e-05,
0.000000e+00, 1.056896e-06, 3.664292e-01, 4.465335e-01, 3.745726e-01, 1.787221e-03)

要在xtable中使用科学记数法,您需要在数字选项中使用负数。

xtable 的数字选项的部分文档:

If values of digits are negative, the corresponding values of x are displayed in scientific format with abs(digits) digits.

示例:

xtable::xtable(data.frame(coeffs, pv), digits = -2)

\begin{table}[ht]
\centering
\begin{tabular}{rrr}
  \hline
 & coeffs & pv \ 
  \hline
1 & -8.13E-02 & 1.96E-02 \ 
  2 & -9.65E-03 & 0.00E+00 \ 
  3 & -1.09E+00 & 0.00E+00 \ 
  4 & 1.15E-02 & 7.96E-01 \ 
  5 & -1.83E-03 & 7.44E-01 \ 
  6 & -1.95E-01 & 6.70E-06 \ 
  7 & -3.71E-02 & 6.26E-01 \ 
  8 & 6.44E-02 & 1.01E-01 \ 
  9 & -1.11E-01 & 4.20E-05 \ 
  10 & -4.34E-03 & 0.00E+00 \ 
  11 & 9.26E-03 & 1.06E-06 \ 
  12 & -1.36E-04 & 3.66E-01 \ 
  13 & -1.32E-04 & 4.47E-01 \ 
  14 & -1.37E-04 & 3.75E-01 \ 
  15 & -5.02E-04 & 1.79E-03 \ 
   \hline
\end{tabular}
\end{table}