R 表列表的xtable输出
我有一个表列表,希望保存它以用于LaTex输出。代码如下:R 表列表的xtable输出,r,sweave,xtable,R,Sweave,Xtable,我有一个表列表,希望保存它以用于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, .margi
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)
要发送三个列联表的输出,我必须编写三个这样的命令。在这种情况下是可行的。但对于我的实际数据,我有很多列联表。我想知道如何更有效地发送所有应急表格。一种选择是只打印列表
表
,而不打印xtable
。但我希望列联表具有良好的输出格式。感谢您的时间和帮助。鉴于缺乏实际数据及其结构,这里有一种方法
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)
@
TabelFn2我需要一些模拟数据来处理
Data <- data.frame(a=rbinom(100,1,0.5), b=rbinom(100,1,0.3), c=rbinom(100,1,0.6))
您可以通过迭代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]])))
})
由于信息不存在,无法自动制作标题。但是,如果修改表fn
函数,则可以添加该信息,然后将其提取出来
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]])))
})
TabelFn@Ramnath,谢谢你的回复。使用给定的代码Rnw文件可以工作,但使用LaTex时,我收到了以下错误消息:LaTex错误:未处理的浮点太多。甚至我也使用了\usepackage[section]{placeins}。任何评论。再次感谢。@Brian Diggs,非常感谢。你总是来救我。再次感谢。@Brian Diggs,如果您能以同样的方式帮助我获得频率,我将不胜感激。我使用这个命令Freq,变量很少,效果很好。但根据我的实际数据,我得到了一个错误:太多未经处理的絮状物。@MYaseen208:对于频率,我会稍微改变一下Freq关于太多未处理的浮点,可以尝试将table.placement
更改为H
,并将\usepackage{float}
添加到swave文件的前导。我不知道它是否会起作用,但它可能会起作用。
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]])))
})
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]])))
})