在 R 中,我们如何向 kable() table 添加重要的星星?

In R, how can we add significance stars to a kable() table?

使用 knitr::kable() 函数通过 rmarkdown 创建一个 .doc table,我们如何从给定的数据帧 (df.b) 添加 "significance stars symbols" (i.e. cutpoints = c(0, .001,.01,.05, .1, 1), symbols = c("^{***}","^{**}","^{*}","^{.}"," ")) 接近存储在另一个数据框中的值 (df.a)?

knitr::kable(df.a)

pander::add.significance.stars() 会以与 类似的方式达到此目的吗?

数据

df.a <-  structure(list(X1 = structure(c(3L, 2L, 4L, 1L), .Label = c("-0.29 (-0.54 - 0.01)", 
"0.27 (-0.03 - 0.53)", "0.73 (0.55 - 0.84)", "0.73 (0.55 - 0.85)"
), class = "factor"), X2 = structure(c(3L, 2L, 4L, 1L), .Label = c("-0.28 (-0.54 - 0.02)", 
"0.27 (-0.03 - 0.53)", "0.69 (0.50 - 0.82)", "0.70 (0.51 - 0.83)"
), class = "factor"), X3 = structure(c(4L, 2L, 3L, 1L), .Label = c("-0.17 (-0.44 - 0.14)", 
"0.09 (-0.22 - 0.38)", "0.31 (0.02 - 0.56)", "0.33 (0.04 - 0.58)"
), class = "factor"), X4 = structure(c(2L, 1L, 3L, 4L), .Label = c("-0.13 (-0.42 - 0.18)", 
"-0.15 (-0.43 - 0.16)", "-0.19 (-0.46 - 0.12)", "0.06 (-0.25 - 0.35)"
), class = "factor"), X5 = structure(c(4L, 1L, 3L, 2L), .Label = c("-0.04 (-0.34 - 0.26)", 
"-0.08 (-0.37 - 0.23)", "0.10 (-0.21 - 0.39)", "0.13 (-0.17 - 0.42)"
), class = "factor"), X6 = structure(c(4L, 2L, 3L, 1L), .Label = c("-0.01 (-0.31 - 0.29)", 
"-0.05 (-0.35 - 0.25)", "0.02 (-0.28 - 0.32)", "0.03 (-0.27 - 0.33)"
), class = "factor"), X7 = structure(c(3L, 2L, 4L, 1L), .Label = c("-0.01 (-0.31 - 0.29)", 
"0.03 (-0.27 - 0.33)", "0.07 (-0.24 - 0.36)", "0.09 (-0.22 - 0.38)"
), class = "factor")), .Names = c("X1", "X2", "X3", "X4", "X5", 
"X6", "X7"), row.names = c(NA, -4L), class = "data.frame")

df.b <- structure(list(X1 = c(3.08365717405223e-08, 0.0770756554180689, 
2.72566160752774e-08, 0.0605816224154108), X2 = c(2.61317231409208e-07, 
0.0768489136831625, 1.34823702424569e-07, 0.0670572043154656), 
    X3 = c(0.0283729574663067, 0.567293645829226, 0.0400289173722133, 
    0.28480941148709), X4 = c(0.333467471946385, 0.399044270238958, 
    0.227949341335651, 0.719402495872123), X5 = c(0.397299963277248, 
    0.800096545563173, 0.523680255857182, 0.610378930021625), 
    X6 = c(0.83554042835763, 0.736278751820815, 0.897275390269942, 
    0.948005387385236), X7 = c(0.663553032940852, 0.855758221285351, 
    0.586796271186126, 0.924437551906055)), .Names = c("X1", 
"X2", "X3", "X4", "X5", "X6", "X7"), row.names = c(NA, -4L), class = "data.frame")

您可以先将两个 data.frame 对象转换为 matrix,以便能够 paste 一个一个地 paste 单元格,然后应用一些格式,例如:

> pander(matrix(paste(as.matrix(df.a),
+                     as.matrix(add.significance.stars(df.b))),
+               nrow = nrow(df.a),
+               dimnames = list(1:nrow(df.a), names(df.a))),
+        split.table = Inf, style = 'rmarkdown', justify = 'left')


| X1                        | X2                        | X3                    | X4                   | X5                   | X6                   | X7                   |
|:--------------------------|:--------------------------|:----------------------|:---------------------|:---------------------|:---------------------|:---------------------|
| 0.73 (0.55 - 0.84)  * * * | 0.69 (0.50 - 0.82)  * * * | 0.33 (0.04 - 0.58)  * | -0.15 (-0.43 - 0.16) | 0.13 (-0.17 - 0.42)  | 0.03 (-0.27 - 0.33)  | 0.07 (-0.24 - 0.36)  |
| 0.27 (-0.03 - 0.53)       | 0.27 (-0.03 - 0.53)       | 0.09 (-0.22 - 0.38)   | -0.13 (-0.42 - 0.18) | -0.04 (-0.34 - 0.26) | -0.05 (-0.35 - 0.25) | 0.03 (-0.27 - 0.33)  |
| 0.73 (0.55 - 0.85)  * * * | 0.70 (0.51 - 0.83)  * * * | 0.31 (0.02 - 0.56)  * | -0.19 (-0.46 - 0.12) | 0.10 (-0.21 - 0.39)  | 0.02 (-0.28 - 0.32)  | 0.09 (-0.22 - 0.38)  |
| -0.29 (-0.54 - 0.01)      | -0.28 (-0.54 - 0.02)      | -0.17 (-0.44 - 0.14)  | 0.06 (-0.25 - 0.35)  | -0.08 (-0.37 - 0.23) | -0.01 (-0.31 - 0.29) | -0.01 (-0.31 - 0.29) |