【发布时间】:2021-10-12 14:25:48
【问题描述】:
我正在尝试格式化表格。我注意到一列没有我想要的格式。在下图中,您可以看到标题为“p-value”的列中的值显示为“1.0x10^-5”。我似乎无法弄清楚为什么该列中的 x 是斜体的。我真正希望该列看起来像这样“1.0 x 10^-5”的每个值都没有斜体 x 和“x”前后的空格。我想知道是否有人对如何解决这个问题有任何想法?我在下面添加了一个示例数据集以及用于创建表的代码。
kbl(df.test, format = "html", table.attr = "style='width:40%;'", escape = F, align = "r") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive")) %>%
kable_classic(full_width = F, html_font = "Times New Roman")
示例数据:
structure(list(SNP = c("abc", "abc", "abc"), Gene = c("xyz",
"xyz", "xyz"), Cases = c(87L, 86L, 72L), `% Population` = structure(19:17, .Label = c("$12.4^{+5.3}_{-4.2}$",
"$12.7^{+5.4}_{-4.3}$", "$13.3^{+5.4}_{-4.4}$", "$13.6^{+5.5}_{-4.4}$",
"$13.9^{+5.5}_{-4.4}$", "$13.9^{+5.5}_{-4.5}$", "$14.2^{+5.5}_{-4.5}$",
"$14.8^{+5.6}_{-4.6}$", "$15.4^{+5.7}_{-4.7}$", "$16.0^{+5.8}_{-4.8}$",
"$16.3^{+5.8}_{-4.8}$", "$17.2^{+5.9}_{-4.9}$", "$19.8^{+6.2}_{-5.3}$",
"$20.4^{+6.2}_{-5.3}$", "$20.7^{6.2}_{-5.4}$", "$21.0^{+6.2}_{-5.4}$",
"$21.3^{+6.3}_{-5.4}$", "$25.4^{+6.6}_{-5.9}$", "$25.7^{+6.6}_{-5.9}$"
), class = "factor"), p.value = structure(c(12L, 14L, 18L), .Label = c("$1.0 x 10^{-5}$",
"$1.1 x 10^{-4}$", "$1.2 x 10^{-5}$", "$1.3 x 10^{-3}$", "$1.4 x 10^{-5}$",
"$1.5 x 10^{-3}$", "$1.7 x 10^{-4}$", "$2.0 x 10^{-4}$", "$2.0 x 10^{-5}$",
"$2.9 x 10^{-4}$", "$4.0 x 10^{-4}$", "$5.2 x 10^{-7}$", "$5.6 x 10^{-4}$",
"$6.3 x 10^{-7}$", "$6.6 x 10^{-4}$", "$6.6 x 10^{-4}$", "$7.8 x 10^{-4}$",
"$8.5 x 10^{-6}$", "$9.2 x 10^{-4}$"), class = "factor")), row.names = c(NA,
3L), class = "data.frame")
【问题讨论】:
-
尝试使用星号
*而不是x以正确显示乘法。有趣的是,以8.5*10^-6的形式呈现 P 值通常不会提供比P < .001更多的信息(取决于您的领域)。 -
我完全同意,但是,我真的只需要这个表格的这种格式。
-
这会产生你想要的吗?
tibble(p.value = "$6.6$ x $10^{-4}$") %>% kbl(format = "html") -
您可以使用
your_data %>% mutate(p.value = gsub(x = p.value, pattern = " x ", replacement = "$ x $")) %>% kbl()测试您的数据 -
你上面提到的有效。随意把它变成一个答案,我会赞成并接受它
标签: r kable kableextra