在R中,我们如何在kable()表中添加重要星? [英] In R, how can we add significance stars to a kable() table?

查看:178
本文介绍了在R中,我们如何在kable()表中添加重要星?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用knitr :: kable()函数通过rmarkdown创建.doc表,我们如何在给定数据帧(df.b)(i.e. cutpoints = c(0, .001,.01,.05, .1, 1), symbols = c("^{***}","^{**}","^{*}","^{.}"," "))中添加显着性星符号",将其存储在另一个数据帧中的值附近(df.a)?

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)

pander::add.significance.stars()会为此目的而工作,其使用方式与

Would pander::add.significance.stars() work on this purpose, in a similar way as used here?

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单元格,然后应用某种格式,例如:

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) |

这篇关于在R中,我们如何在kable()表中添加重要星?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆