【问题标题】:Multi-row latex table with R带 R 的多行乳胶表
【发布时间】:2019-09-11 15:31:22
【问题描述】:

我有这个数据框:

mydf <- data.frame(rowFactor1 = c(rep("S4",6),rep("S5",6),rep("S6",6)),
                   rowFactor2 = rep(c("All","T1/T2(V)"), 9),
                   colFactor1 = rep(c(rep("Ml",2),rep("Mp",2),rep("Mra",2)), 3), 
                   x = c(
                     "8.73",
                     "0.46 / 8.28 ( 2 )",
                     "7.02",
                     "0.84 / 8.28 ( 3 )",
                     "0.55",
                     "0.90 / 1.20 ( 0 )",
                     "8.14",
                     "2.02 / 1.57 ( 5 )",
                     "0.55",
                     "2.90 / 1.28 ( 4 )",
                     "1.73",
                     "3.46 / 8.28 ( 6 )",
                     "6.02",
                     "0.87 / 8.90 ( 5 )",
                     "1.55",
                     "0.90 / 1.20 ( 1 )",
                     "6.24",
                     "2.02 / 1.57 ( 7 )"
                   ))

如何在 R 上使用 mydf 创建具有多行的 Latex 表?

我尝试使用R包tablestabular函数

tab1 <- tabular(Heading()*RowFactor(rowFactor1, spacing = 1, 
                                    c("S4","S5","S6"))*
                  Heading()*RowFactor(rowFactor2, 
                                      levelnames = c("All","T1/T2(V)")) ~ 
                  Heading()*Factor(colFactor1, 
                                   levelnames = c("Ml","Mp","Mra")),
                data = mydf)
latex(tab1)

我有这张表没有 x 的值 (8.73, 0.46 / 8.28 ( 2 ) ...) 并填充了单个值 1

感谢您的帮助!

【问题讨论】:

  • 我想你在找library (tidyr) mydf %&gt;% spread(colFactor1, x)
  • @tmfmnk 谢谢你的建议,但我需要一个使用 R 的 Latex 表。
  • @FloraGrappelli 请不要编辑帖子以添加感谢行。请阅读meta.stackoverflow.com/a/328380/2777074

标签: r latex character tabular multirow


【解决方案1】:

如果您希望在表格中使用字符串,可以将您的 x 因子转换为字符:

mydf$x <- as.character(mydf$x)

然后包含x 字符串,如下所示:

library(tables)

tab1 <- tabular(Heading()*RowFactor(rowFactor1, spacing = 1, 
                                    c("S4","S5","S6"))*
                  Heading()*RowFactor(rowFactor2, 
                                      levelnames = c("All","T1/T2(V)")) ~ 
                  Heading()*Factor(colFactor1, 
                                   levelnames = c("Ml","Mp","Mra"))*
                  Heading()*(x)*Heading()*identity,
                  data = mydf)
latex(tab1)

还可以考虑替代 tabular 用于 Latex 表:

Tools for making latex tables in R

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多