【问题标题】:Output a table with subtables in latex with xtable使用 xtable 在乳胶中输出带有子表的表
【发布时间】:2014-06-04 20:43:48
【问题描述】:

我想创建一个包含许多不同子表的乳胶表。我基本上想生成一个包含调查答案的表格。

例如第一个问题的表格是:

Q1 <- structure(c(6L, 14L, 20L, 15L, 2L, 3L, 12L, 25L, 7L, 1L, 2L, 
13L, 35L, 10L, 3L), .Dim = c(5L, 3L), .Dimnames = structure(list(
    c("none", "very little", "some", "most", "all"), c("control", 
    "treatment1", "treatment2")), .Names = c("", "")), class = "table")

第二个问题的表格是:

Q2 <- structure(c(39L, 12L, 4L, 1L, 1L, 31L, 13L, 4L, 0L, 0L, 39L, 
20L, 4L, 0L, 0L), .Dim = c(5L, 3L), .Dimnames = structure(list(
    c("A", "B+", "B", "B-", "C"), c("control", "treatment1", 
    "treatment2")), .Names = c("", "")), class = "table")

我希望我的 LaTeX 输出看起来像:

                    Some Title
                    Question 1
             control treatment1 treatment2
  none              6          3          2
  very little      14         12         13
  some             20         25         35
  most             15          7         10
  all               2          1          3
                    Question 2
  A                39         31         39
  B+               12         13         20
  B                 4          4          4
  B-                1          0          0
  C                 1          0          0

我可以使用 X 表创建单独的表,但这需要在乳胶中进行一些手动工作才能合并它们。现在我做:

print(xtable(Q1), floating = FALSE, only.contents = FALSE, 
      include.rownames = TRUE, include.colnames = TRUE, hline.before = c(1))

【问题讨论】:

  • 我会将rbind 表格合并到一个表格中,并在print(xtable()) 命令中使用add.to.row 功能包含中间标头(有关如何使用它,请参阅this post)。对于中间标头的居中,我建议使用 multirow 包功能。上面提到的帖子中有一条评论解释了如何应用它。

标签: r xtable


【解决方案1】:

虽然这种方法是一种解决方法,但它与您正在寻找的方法很接近。为了获得一个可行的结构,我需要使用 Epitools 包中的 expand.table 扩展您的表格,并使用表格包中的 tabular 函数来获得可用的格式。这可能不是最好或最有效的方法,但如果您试图避免稍后进行一些乳胶处理,这可能会有所帮助。过滤并转换为矩阵后,您可以使用 xtable 生成下表:

R 代码

require(xtable)
require(epitools)
require(tables)

Q1 <- structure(c(6L, 14L, 20L, 15L, 2L, 3L, 12L, 25L, 7L, 1L, 2L, 
                  13L, 35L, 10L, 3L), .Dim = c(5L, 3L), .Dimnames = structure(list(
                    c("none", "very little", "some", "most", "all"), c("control", 
                                                                       "treatment1", "treatment2")), .Names = c("", "")), class = "table")

Q2 <- structure(c(39L, 12L, 4L, 1L, 1L, 31L, 13L, 4L, 0L, 0L, 39L, 
                  20L, 4L, 0L, 0L), .Dim = c(5L, 3L), .Dimnames = structure(list(
                    c("A", "B+", "B", "B-", "C"), c("control", "treatment1", 
                                                    "treatment2")), .Names = c("", "")), class = "table")


names(dimnames(Q1)) <- c("Responses", "Conditions")
names(dimnames(Q2)) <- c("Responses", "Conditions")
q1_df <- expand.table(Q1)
q1_df$Question <- "Question 1"
q2_df <- expand.table(Q2)
q2_df$Question <- "Question 2"
df <- rbind(q1_df, q2_df)


tab <- tabular((Factor(Question)*Factor(Responses)+1) ~ (Conditions), data=df)
tab <- tab[tab[,1] > 0,]
tab <- as.matrix(tab[1:nrow(tab) -1,])

print(xtable(tab, caption="Some Title"), include.rownames=F, include.colnames=F, latex.environments="center", comment=F, caption.placement='top', hline.after=c(0, nrow(tab)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 2017-07-18
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 2022-10-14
    相关资源
    最近更新 更多