2017-07-25 230 views
1

用什么方式可以在Rmarkdown中构建双向频率表? 喜欢的东西:Rmarkdown中的双向频率表

this

我试图同时使用来自knitr包kable函数,从DT包确定年代的功能,但没有给我想要的结果。

更新: 带有示例的可重复代码。

a <- sample(x = c(1,2,3), size = 1000, replace = T) 
b <- sample(x = c('a', 'b', 'c'), size = 1000, replace = T) 

df <- data.frame(vertical_title = a, horitzontal_title = b) 

table(df) 

       horitzontal_title 
vertical_title a b c 
      1 118 98 106 
      2 106 95 121 
      3 128 114 114 

我希望'vertical_title'和'horizo​​ntal_title'对我的Rmarkdown表格可见。

+0

你的问题很难回答这个问题。请参阅[这里](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example),了解如何提供可重现示例的提示。 – Florian

+0

弗洛里安好点,我有点匆忙。我添加了一个可重现的例子。 – Michael

回答

0

可以构造表如下:

--- 
title: Example 1 
output: pdf_document 
--- 

### This is a test 

```{r,results='asis',echo=FALSE} 

library(xtable) 
a <- sample(x = c(1,2,3), size = 1000, replace = T) 
b <- sample(x = c('a', 'b', 'c'), size = 1000, replace = T) 

df <- data.frame(vertical_title = a, horitzontal_title = b) 
df <- as.data.frame.matrix(table(df)) 


print(xtable(df, 
      align = "|l|rrr|", 
      caption = "Test"), 
     caption.placement = 'top', 
     comment = F, 
     scalebox=1, 
     include.rownames = T, 
     hline.after = c(-1,0,nrow(df)), 
     vline.after = c(1), 
     format.args=list(big.mark = ",", decimal.mark = ".")) 


``` 

在这里看到了添加多列和multirows你的标题:

R package xtable, how to create a latextable with multiple rows and columns from R

希望这有助于!