问题描述
我正在尝试在我的 RMarkdown pdf 文档中创建一个基本的描述性汇总表。
data(iris)
library(summarytools)
iris %>%
group_by(Species) %>%
descr(Sepal.Length,stats = "fivenum",headings = FALSE)
我得到以下输出:
Descriptive Statistics
Sepal.Length by Species
Data Frame: iris
N: 50
setosa versicolor virginica
------------ -------- ------------ -----------
Min 4.30 4.90 4.90
Q1 4.80 5.60 6.20
Median 5.00 5.90 6.50
Q3 5.20 6.30 6.90
Max 5.80 7.00 7.90
如何从最终输出中去掉这部分?:
Descriptive Statistics
Sepal.Length by Species
Data Frame: iris
N: 50
我以为 headings = FALSE
会这样做,但我想我错了!任何帮助将不胜感激。
解决方法
应该很快修复,但与此同时,这会起作用:
iris %>%
group_by(Species) %>%
descr(Sepal.Length,stats = "fivenum") %>%
print(headings = FALSE)
## setosa versicolor virginica
## ------------ -------- ------------ -----------
## Min 4.30 4.90 4.90
## Q1 4.80 5.60 6.20
## Median 5.00 5.90 6.50
## Q3 5.20 6.30 6.90
## Max 5.80 7.00 7.90
,
我会使用 tb
去掉上面的“描述性”部分,然后使用 kable
为 Rmarkdown
输出制作一个漂亮的表格。例如:
library(summarytools)
library(dplyr)
iris %>%
group_by(Species) %>%
descr(Sepal.Length,stats = "fivenum") %>%
tb() %>%
knitr::kable()
输出:
|Species |variable | min| q1| med| q3| max|
|:----------|:------------|---:|---:|---:|---:|---:|
|setosa |Sepal.Length | 4.3| 4.8| 5.0| 5.2| 5.8|
|versicolor |Sepal.Length | 4.9| 5.6| 5.9| 6.3| 7.0|
|virginica |Sepal.Length | 4.9| 6.2| 6.5| 6.9| 7.9|
我们也可以使用 t()
转置表格。例如:
iris %>%
group_by(Species) %>%
descr(Sepal.Length,stats = "fivenum") %>%
tb() %>%
t() %>%
knitr::kable()
| | | | |
|:--------|:------------|:------------|:------------|
|Species |setosa |versicolor |virginica |
|variable |Sepal.Length |Sepal.Length |Sepal.Length |
|min |4.3 |4.9 |4.9 |
|q1 |4.8 |5.6 |6.2 |
|med |5.0 |5.9 |6.5 |
|q3 |5.2 |6.3 |6.9 |
|max |5.8 |7.0 |7.9 |