Search code examples
rprintingr-markdownsummaryskimr

Change the order in which summary functions are printed by skim


I'm using skimr, and I added two summary functions (iqr_na_rm and median_na_rm) to the list of summary functions for the function skim. However, by default these new summary functions (called skimmers in skimr documentation) appear at the end of the table. Instead, I'd like median and iqr to appear after mean and sd.

The final goal is to show the results in a .Rmd report like this:

---
title: "Test"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, 
                      message = FALSE,
                      echo    = FALSE)
```

## Test

```{r test, results = 'asis'}
library(skimr)
library(dplyr)
library(ggplot2)

iqr_na_rm <- function(x) IQR(x, na.rm = TRUE)
median_na_rm <- function(x) median(x, na.rm = TRUE)

skim_with(numeric = list(p50 = NULL, median = median_na_rm, iqr = iqr_na_rm),
          integer = list(p50 = NULL, median = median_na_rm, iqr = iqr_na_rm))

msleep %>%
  group_by(vore) %>%
  skim(sleep_total) %>%
  kable()

```

Rendered HTML:

enter image description here

As you can see, median and iqr are printed and the end of the table, after the sparkline histogram. I'd like them to be printed after sd and before p0. Is it possible?


Solution

  • There are two parts in the skim() output. If you want to control the numeric part, you can use skim_to_list like this. It's also easier to export in another format.

    msleep %>%
      group_by(vore) %>%
      skim_to_list(sleep_total)%>%
      .[["numeric"]]%>%
      dplyr::select(vore,variable,missing,complete,n,mean,sd,
                    median,iqr,p0,p25,p75,p100,hist)
    
    # A tibble: 5 x 14
      vore    variable    missing complete n     mean    sd     median iqr     p0    p25    p75     p100   hist    
    * <chr>   <chr>       <chr>   <chr>    <chr> <chr>   <chr>  <chr>  <chr>   <chr> <chr>  <chr>   <chr>  <chr>   
    1 carni   sleep_total 0       19       19    10.38   4.67   10.4   " 6.75" 2.7   6.25   "13   " 19.4   ▃▇▂▇▆▃▂▃
    2 herbi   sleep_total 0       32       32    " 9.51" 4.88   10.3   " 9.92" 1.9   "4.3 " 14.22   16.6   ▆▇▁▂▂▆▇▅
    3 insecti sleep_total 0       5        5     14.94   5.92   18.1   "11.1 " 8.4   "8.6 " "19.7 " 19.9   ▇▁▁▁▁▁▃▇
    4 omni    sleep_total 0       20       20    10.93   2.95   " 9.9" " 1.83" "8  " "9.1 " 10.93   "18  " ▆▇▂▁▁▁▁▂
    5 NA      sleep_total 0       7        7     10.19   "3   " 10.6   " 3.5 " 5.4   8.65   12.15   13.7   ▃▃▁▁▃▇▁▇
    

    EDIT

    Adding kable() as requested in comment.

    msleep %>%
      group_by(vore) %>%
      skim_to_list(sleep_total)%>%
      .[["numeric"]]%>%
      dplyr::select(vore,variable,missing,complete,n,mean,sd,median,iqr,p0,p25,p75,p100,hist)%>%
      kable()
    
    |  vore   |  variable   | missing | complete | n  | mean  |  sd  | median | iqr  | p0  | p25  |  p75  | p100 |   hist   |
    |---------|-------------|---------|----------|----|-------|------|--------|------|-----|------|-------|------|----------|
    |  carni  | sleep_total |    0    |    19    | 19 | 10.38 | 4.67 |  10.4  | 6.75 | 2.7 | 6.25 |  13   | 19.4 | ▃▇▂▇▆▃▂▃ |
    |  herbi  | sleep_total |    0    |    32    | 32 | 9.51  | 4.88 |  10.3  | 9.92 | 1.9 | 4.3  | 14.22 | 16.6 | ▆▇▁▂▂▆▇▅ |
    | insecti | sleep_total |    0    |    5     | 5  | 14.94 | 5.92 |  18.1  | 11.1 | 8.4 | 8.6  | 19.7  | 19.9 | ▇▁▁▁▁▁▃▇ |
    |  omni   | sleep_total |    0    |    20    | 20 | 10.93 | 2.95 |  9.9   | 1.83 |  8  | 9.1  | 10.93 |  18  | ▆▇▂▁▁▁▁▂ |
    |   NA    | sleep_total |    0    |    7     | 7  | 10.19 |  3   |  10.6  | 3.5  | 5.4 | 8.65 | 12.15 | 13.7 | ▃▃▁▁▃▇▁▇ |