R xtable несколько строк и несколько строк заметок (объединение xtableList и объединение ячеек) - PullRequest
0 голосов
/ 26 мая 2020

Я пытаюсь создать таблицу с (1) многослойными заголовками столбцов и (2) несколькими строками примечаний прямо под bottomrule. Я мог бы выполнить sh (1), следуя ответу @ ToJo в этом посте ( R package xtable, как создать латекстовый файл с несколькими строками и столбцами из R ). И я мог бы получить (вроде) то, что хочу (2), следуя ответу @ franzbischoff в этом посте ( R: xtable caption (or comment) ). Проблема возникает, когда я пытаюсь совместить их. Вот воспроизводимый пример.

test <- structure(list(dep = c("Sample1", "var1", 
"var2", "var3", "var4", "", "Sample2", 
"var1", "var3"), IN = c("", "11.83", "33.47", "3211", 
"1.52", "", "", "11.58", "1868"), in2 = c("", "0.0045", "0.1782", 
"14.31", "0.0029", "", "", "0.01", "16.77"), OUT = c("", "11.72", 
"35.92", "3291", "1.55", "", "", "11.65", "1919"), out2 = c("", 
"0.0008", "0.0348", "3.08", "0.0006", "", "", "0.0052", "7.96"
), Estimate = c("", "0.1073", "-2.45", "-79.99", "-0.0336", "", 
"", "-0.0746", "-51.24"), `t value` = c("", "1.84", "-1.11", 
"-0.4705", "-1.94", "", "", "-0.899", "-1.12")), row.names = c(NA, 
-9L), class = "data.frame")

Теперь я создаю таблицу на основе этого.

print(xtable(test, 
             align = "llcccccc",  # align and put a vertical line (first "l" again represents column of row numbers)
             caption = "Test Table", label = "table:test"),
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      hline.after=NULL, #We don't need hline; we use booktabs
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      add.to.row = list(pos = list(-1, nrow(test)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          " & \\multicolumn{2}{c}{\\textbf{IN}} & \\multicolumn{2}{c}{\\textbf{OUT}} & \\multicolumn{2}{c}{\\textbf{Difference}} \\\\\n",
                                          " Variables & Mean & SE & Mean & SE & Mean & t-stat \\\\\n", # NEW row 
                                          "\\midrule \n"),
                                    paste("\\bottomrule \n")) ))

Я получаю (1) как следующее: enter image description here

Но когда я пытаюсь добавить примечания под таблицей, используя xtableList после @ franzbischoff в этом сообщении ( R: заголовок xtable (или комментарий) ), похоже, не работает. Сначала создайте xtableList, используя набор данных.

dfList <- list(test)
attr(dfList, "message") <- c("Note: Standard errors are clustered at the community (municipality) level. All the variables are from 1980 Census.")

Печать dfList выдает сообщение об ошибке.

print(xtableList(dfList, 
             align = "llcccccc",  # align and put a vertical line (first "l" again represents column of row numbers)
             caption = "Test Table", label = "table:test"),
      include.rownames = FALSE, #Don't print rownames
      include.colnames = FALSE, #We create them ourselves
      caption.placement = "top", #"top", NULL
      hline.after=NULL, #We don't need hline; we use booktabs
      floating=TRUE, # whether \begin{Table} should be created (TRUE) or not (FALSE)
      add.to.row = list(pos = list(-1, nrow(test)),
                        command = c(paste("\\toprule \n",  # NEW row
                                          " & \\multicolumn{2}{c}{\\textbf{IN}} & \\multicolumn{2}{c}{\\textbf{OUT}} & \\multicolumn{2}{c}{\\textbf{Difference}} \\\\\n",
                                          " Variables & Mean & SE & Mean & SE & Mean & t-stat \\\\\n", # NEW row 
                                          "\\midrule \n"),
                                    paste("\\bottomrule \n")) ))
#> Error in print.xtable(combined, type = type, floating = floating, floating.environment = floating.environment, : formal argument "include.colnames" matched by multiple actual arguments

Вот пример таблицы, которую я хочу в конечном итоге. Он имеет как (1), так и (2). Для (2) примечание создается в пределах ширины таблицы с автоматическими c изменениями строк. Было бы здорово, если бы я мог сделать это с помощью xtable! Спасибо!

enter image description here

...