[英]Output a table with subtables in latex with xtable
我想創建一個包含許多不同子表的乳膠表。 我基本上想要生成一個包含調查答案的表格。
例如,第一個問題的表是:
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表創建單獨的表,但這需要在latex中進行一些手動工作來合並它們。 現在我做:
print(xtable(Q1), floating = FALSE, only.contents = FALSE,
include.rownames = TRUE, include.colnames = TRUE, hline.before = c(1))
雖然這種方法有點變通方法,但它接近您所尋找的方法。 為了得到一個可行的結構,我需要與擴大你的表expand.table
從epitools包,並使用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)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.