【问题标题】:Two way frequency table in RmarkdownRmarkdown中的两路频率表
【发布时间】:2017-12-31 16:21:08
【问题描述】:

在 Rmarkdown 中如何构建双向频率表? 比如:

我尝试使用 knitr 包中的 kable 函数和 DT 包中的 datable 函数,但都没有给我想要的结果。

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

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 中对我的表格可见。

【问题讨论】:

  • 你的问题很难这样回答。请参阅here 以获取有关如何提供可重现示例的提示。
  • 好点弗洛里安,我有点着急。我添加了一个可重现的示例。

标签: r knitr r-markdown contingency


【解决方案1】:

您可以按如下方式构建表格:

---
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 = "."))


```

请参阅此处了解为您的标题添加多列和多行的方法:

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

希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 2016-12-09
    • 2012-06-08
    • 1970-01-01
    相关资源
    最近更新 更多