2

I am trying to create a basic descriptive summary table in my RMarkdown pdf document.

data(iris)
library(summarytools)

iris %>%
  group_by(Species) %>%
  descr(Sepal.Length, stats = "fivenum", headings = FALSE)

I get the following output:

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

How do I get rid of this part from the final output?:

Descriptive Statistics  
Sepal.Length by Species  
Data Frame: iris  
N: 50

I assumed headings = FALSE was going to do it, but I guess I was wrong! Any help would be much appreciated.

hy9fesh
  • 589
  • 2
  • 15

2 Answers2

1

Should be fixed soon, but in the meantime, this will work:

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
Dominic Comtois
  • 10,230
  • 1
  • 39
  • 61
0

I would use tb to get rid of the "descriptive" part above and then kable for making a nice table for Rmarkdown outputs. For example:

library(summarytools)
library(dplyr)

iris %>%
        group_by(Species) %>%
        descr(Sepal.Length, stats = "fivenum") %>%
        tb() %>%
        knitr::kable()

Output:

|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|

We could also transpose the table using t(). For example:

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          |
bird
  • 2,938
  • 1
  • 6
  • 27