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には 3 つの分割表があり、次のコードを使用して出力を 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')))
@

3 つの分割表の出力を送信するには、そのようなコマンドを 3 つ作成する必要があります。この場合は実現可能です。しかし、実際のデータについては、多くの分割表があります。すべての分割表をより効率的に送信する方法を知りたいです。選択肢の 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

実際のデータとその構造がないことを考えると、ここに 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 に答える