在R中有效地表示非常小的数字

在R中有效地表示非常小的数字,r,presentation,R,Presentation,想象一下这个表,其中每一行是一个不同的变量,列是系数和p值: Coef P-Value [1,] -0.0005017369 1.787221e-03 [2,] -0.0096543344 0.000000e+00 当我使用xtable插入latex xtable(表,数字=3)时,我得到以下结果: \begin{table}[ht] \centering \begin{tabular}{rrr} \hline & 1 & 2 \\

想象一下这个表,其中每一行是一个不同的变量,列是系数和p值:

           Coef       P-Value
 [1,] -0.0005017369 1.787221e-03
 [2,] -0.0096543344 0.000000e+00
当我使用xtable插入latex xtable(表,数字=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
Coef
[1,]-5.01 e^-4(***)
[2,]-9.65 e^-3(***)
当P值<0.01时带(***)
(**)当0.01
以下是示例的一些数据:

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)

coefs要在
xtable
中使用科学符号,您需要在digits选项中使用负数

xtable
数字选项的部分文档:

如果数字值为负数,则对应的x值为 以科学格式显示,带有abs(数字)数字

例如:

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}

你能先转换成字符吗?类似于
mat[]的东西,这与使用
数字=4或6不一样吗?非常小的值仍将显示为
0.0000
等。重点是使用
e-4
来表示,例如计算四位小数等。这将是格式
g
,请参阅
帮助(“sprintf”)
。这不适用于在数字旁边添加
(***)
,与使用
paste
stars.pval
命令一样。它使用
格式(table,format=“e”,digits=2)
。只是为了让答案完整。