简体   繁体   中英

Output a table with subtables in latex with xtable

I want to create a latex table with many different subtables. I basically want to generate a table with the answers from a survey.

For example the table for the first question is:

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

The table for the second question is:

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

I want my LaTeX output to look something like:

                    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

I can create individual tables with X table but that requires some manual work in latex to merge them. Right now I do:

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

Although this method is a bit of a workaround, it gets close to what you are looking for. In order to get a workable structure, I needed to expand your tables with expand.table from the epitools package and use the tabular function from tables package to get a useable format. This is may not be the best or most efficient way but if you are trying to avoid some later latex processing this could help. After filtering and converting to a matrix you can use xtable to produce the following table:

在此输入图像描述

R code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM