问题
Using knitr::kable() function to create a .doc table through rmarkdown, how could we add "significance stars symbols" from a given data frame (df.b) (i.e. cutpoints = c(0, .001,.01,.05, .1, 1), symbols = c("^{***}","^{**}","^{*}","^{.}"," "))
near the values stored in another data frame (df.a)?
knitr::kable(df.a)
Would pander::add.significance.stars()
work on this purpose, in a similar way as used here?
Data
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")
回答1:
You can convert the two data.frame
objects to matrix
first to be able to paste
the cells one by one, then apply some formatting, eg:
> 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) |
来源:https://stackoverflow.com/questions/43345761/in-r-how-can-we-add-significance-stars-to-a-kable-table