9

我正在使用knit::kableand打印频率表pander::pandoc,通常这对于 HTML/Word/LaTeX 输出非常有用。但有时我想在最终产品中保留维度名称。不幸的是,当转换为降价时,两者都删除了panderknitr

# create a simple table
tab <- table(mtcars$gear, mtcars$carb)

# add dimension names
names(dimnames(tab)) <- c("gear", "carb")

这将创建一个表:

    carb
gear 1 2 3 4 6 8
   3 3 4 3 5 0 0
   4 4 4 0 4 0 0
   5 0 2 0 1 1 1

但是现在如果我们使用 kable 进行打印:

> kable(tab)

|   |  1|  2|  3|  4|  6|  8|
|:--|--:|--:|--:|--:|--:|--:|
|3  |  3|  4|  3|  5|  0|  0|
|4  |  4|  4|  0|  4|  0|  0|
|5  |  0|  2|  0|  1|  1|  1|

没有维度名称!(并且?kable不表示任何包含它们的选项。)

对可以保留这些的工具有什么建议吗?我注意到这descr:CrossTable可以解决问题,但包含很多我想省略的额外信息。

非常感谢。

4

1 回答 1

8

您可以使用例如ftable隐式创建具有维度名称的平面列联表:

> pander::pander(ftable(tab))

---- ---- - - - - - -
     carb 1 2 3 4 6 8

gear                 

 3        3 4 3 5 0 0

 4        4 4 0 4 0 0

 5        0 2 0 1 1 1
---- ---- - - - - - -

或者您也可以抑制不需要的单元格,descr::CrossTable例如:

> pander(descr::CrossTable(tab, prop.r = FALSE, prop.c = FALSE, prop.chisq = FALSE))

------------------------------------------------------------------------------
 &nbsp;\   carb\    &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\ 
  gear       1         2         3         4         6         8       Total  
--------- -------- --------- --------- --------- --------- --------- ---------
 **3**\   &nbsp;\  &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\  
   N\       3\       4\        3\        5\        0\        0\         15\   
Total(%)   9.375%   12.500%   9.375%    15.625%   0.000%    0.000%            

 **4**\   &nbsp;\  &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\  
   N\       4\       4\        0\        4\        0\        0\         12\   
Total(%)  12.500%   12.500%   0.000%    12.500%   0.000%    0.000%            

 **5**\   &nbsp;\  &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\   &nbsp;\  
   N\       0\       2\        0\        1\        1\        1\         5\    
Total(%)   0.000%   6.250%    0.000%    3.125%    3.125%    3.125%            

  Total      7        10         3        10         1         1        32    
------------------------------------------------------------------------------

或者在GH上提交一张票:)

于 2015-12-31T22:29:17.633 回答