使用 stargazer 在 table 中自动缩放数字

Automatically scaling numbers in a table using stargazer

我希望创建一些具有不同尺度的数字元素的表格。例如,表格中各种变量的方差沿对角线向下,相关性沿对角线向下。较大的数字会使表格太大,并且更难阅读。

是否有使用 stargazer(或其他类似程序包)缩小更大元素的好方法,并用脚注指出,或自动使用指数表示法?

例如,以下 r 代码创建了一个矩阵,其中对角线元素比其他元素大得多。

x <- matrix(rnorm(25,0,1),5,5)
diag(x) <- rnorm(5,10000000,10)
stargazer(x,summary=F,digits=2)

非常感谢任何帮助。

也许你可以对此做一些改编:

ifelse(x < 100, sprintf("%0.2f", x), sprintf("%0.5e", x))
#    [,1]          [,2]          [,3]          [,4]          [,5]         
#[1,] "9.99999e+06" "-0.79"       "-0.56"       "0.91"        "-2.57"      
#[2,] "-0.13"       "9.99999e+06" "-1.83"       "-0.34"       "1.73"       
#[3,] "-0.48"       "0.38"        "1.00000e+07" "1.40"        "-0.32"      
#[4,] "-0.05"       "-0.62"       "0.91"        "1.00000e+07" "1.15"       
#[5,] "-0.09"       "-0.33"       "-0.16"       "0.35"        "9.99999e+06"

或者,不带引号:

noquote(ifelse(x < 100, sprintf("%0.2f", x), sprintf("%0.5e", x)))
#     [,1]        [,2]        [,3]        [,4]        [,5]       
#[1,] 9.99999e+06 -0.79       -0.56       0.91        -2.57      
#[2,] -0.13       9.99999e+06 -1.83       -0.34       1.73       
#[3,] -0.48       0.38        1.00000e+07 1.40        -0.32      
#[4,] -0.05       -0.62       0.91        1.00000e+07 1.15       
#[5,] -0.09       -0.33       -0.16       0.35        9.99999e+06

您实际上是在转换为文本以按照您想要的方式进行打印。有关输出选项的详细信息,请参阅 ?sprintf.