我有一个表列表,想要为LaTex输出编写它。这是代码:
Data <- esoph[ , 1:3]
library(plyr)
combos <- combn(ncol(Data),2)
TabelFn <- function(x) {
Table <- addmargins(table(Data[, x[1]], Data[, x[2]]))
return(Table)
}
Table <- alply(.data=combos, .margins=2, .fun=TabelFn, .expand=TRUE)
library(xtable)
在这种情况下,列表Table
有三个列联表,我可以使用以下代码将输出扫描到LaTex:
<< label = tabTable, echo = FALSE, results = tex >>=
print(xtable(Table[1]$'1', caption = "Contingency table for agegp and alcgp", label = "tab:Table[1]",
digits = c(0, rep(0, ncol(Table[1]$'1'))),
align = paste(paste("l|", paste(rep("r", ncol(Table[1]$'1')-1), collapse = ''), sep = ""), "l", sep = "")),
table.placement = "tbp", caption.placement = "top",
hline.after = c(-1, 0, nrow(Table[1]$'1')))
@
要发送三个列联表的输出,我要编写三个这样的命令。在这种情况下,这是可行的。但是对于我的实际数据,我有很多列联表。我想知道如何更有效地发送所有列联表。一种选择是只打印Table
列表而不使用xtable
。但是我想要一个很好的输出格式的列联表。感谢您的时间和帮助。
答案 0 :(得分:4)
我需要一些模拟数据才能使用
Data <- data.frame(a=rbinom(100,1,0.5), b=rbinom(100,1,0.3), c=rbinom(100,1,0.6))
使用您的代码生成Table
,这将让您关闭
l_ply(Table, function(TBL) {
print(xtable(TBL,
caption = "Contingency table for agegp and alcgp", #This information is not in the TBL anywhere
label = "tab:Table[1]", # This is also problematic
digits = c(0, rep(0, ncol(TBL))),
align = paste(paste("l|", paste(rep("r", ncol(TBL)-1), collapse = ''), sep = ""), "l", sep = "")),
table.placement = "tbp",
caption.placement = "top",
hline.after = c(-1, 0, nrow(TBL)))
})
您可以通过迭代索引Table
而不是Table
本身来获得标签
a_ply(seq_along(Table), 1, function(i) {
print(xtable(Table[[i]],
caption = "Contingency table for agegp and alcgp", #This information is not in the Table[[i]] anywhere
label = paste("tab:Table[",i,"]",sep=""),
digits = c(0, rep(0, ncol(Table[[i]]))),
align = paste(paste("l|", paste(rep("r", ncol(Table[[i]])-1), collapse = ''), sep = ""), "l", sep = "")),
table.placement = "tbp",
caption.placement = "top",
hline.after = c(-1, 0, nrow(Table[[i]])))
})
由于信息不存在,因此无法自动生成标题。但是,如果您修改了TableFn
功能,则可以添加该信息,然后将其解压缩。
TabelFn <- function(x) {
Table <- addmargins(table(Data[, x[1]], Data[, x[2]]))
names(attr(Table,"dimnames")) <- names(Data)[x]
return(Table)
}
Table <- alply(.data=combos, .margins=2, .fun=TabelFn, .expand=TRUE)
a_ply(seq_along(Table), 1, function(i) {
vars <- names(attr(Table[[i]],"dimnames"))
print(xtable(Table[[i]],
caption = paste("Contingency table for", vars[1], "and", vars[2]),
label = paste("tab:Table[",i,"]",sep=""), # This is also problematic
digits = c(0, rep(0, ncol(Table[[i]]))),
align = paste(paste("l|", paste(rep("r", ncol(Table[[i]])-1), collapse = ''), sep = ""), "l", sep = "")),
table.placement = "tbp",
caption.placement = "top",
hline.after = c(-1, 0, nrow(Table[[i]])))
})
答案 1 :(得分:1)
鉴于缺乏实际数据及其结构,这里有一种方法。
TabelFn2 <- function(x) {
Table <- addmargins(table(Data[, x[1]], Data[, x[2]]))
print(xtable(Table$'1', caption = "Contingency table for agegp and alcgp",
label = "tab:Table", digits = c(0, rep(0, ncol(Table$'1'))),
align = paste(paste("l|", paste(rep("r", ncol(Table$'1')-1),
collapse = ''), sep = ""), "l", sep = "")),
table.placement = "tbp", caption.placement = "top",
hline.after = c(-1, 0, nrow(Table$'1')))
}
<<echo = F, results = tex>>=
a_ply(.data=combos, .margins=2, .fun=TabelFn2)
@