2011-09-14 80 views
2

我有一個表的列表,並希望爲LaTex輸出發送它。下面是代碼:xtable輸出表的列表

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在這種情況下爲三個應急表,我可以使用此代碼輸出sweave乳膠:

<< 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'))) 
@ 

要發送三個應急表的輸出I已經寫了三個這樣的命令。在這種情況下,這是可行的。但對於我的實際數據,我有許多應急表。我想知道如何更有效地發送所有應急表格。一種選擇是僅打印Table而不需要xtable。但我希望有很好的輸出格式的應變表。感謝您的時間和幫助。

回答

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]])))  
}) 
+0

@ Brian Diggs,非常感謝。你總是來救我。再次感謝。 – MYaseen208

+0

@ Brian Diggs,如果你能幫助我以相同的方式獲得頻率,我將非常感激。我使用這個命令Freq < - alply(.data = Data,.margins = 2,.fun = table,.expand = TRUE)並且無法分配名稱。謝謝你的幫助。 – MYaseen208

+0

很少有變量可以正常工作。但與我的實際數據,我得到LaTeX錯誤:太多未處理的floa ts。 – MYaseen208

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) 
@ 
+0

@ Ramnath爲您的答覆。使用你給定的代碼Rnw文件的作品,但與LaTex我得到這個錯誤消息:LaTeX錯誤:太多未處理的浮游物。即使我使用\ usepackage [section] {placeins}。任何意見。再次感謝。 – MYaseen208