2

我有一个表格列表,并希望将其用于 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. 但我希望列联表具有良好的输出格式。感谢您的时间和帮助。

4

2 回答 2

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]])))       
})
于 2011-09-14T23:57:24.873 回答
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)
@
于 2011-09-14T23:33:50.460 回答