我有一个表的列表,并希望将其用于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')))
@要发送三个列联表的输出,我必须编写三个这样的命令。在这种情况下,它是可行的。但对于我的实际数据,我有许多列联表。我想知道如何更有效地发送所有的应急表。一种选择是只打印不带xtable的列表Table。但是我想要一个输出格式良好的列联表。感谢您的时间和帮助。
发布于 2011-09-15 07:57:25
我需要一些模拟数据来处理
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]])))
})发布于 2011-09-15 07:33:50
考虑到缺乏实际数据及其结构,这里是一种方法。
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)
@https://stackoverflow.com/questions/7424159
复制相似问题