更改浏览功能打印摘要功能的顺序

时间:2019-03-18 15:15:36

标签: r printing r-markdown summary skimr

我正在使用skimr,并且向函数iqr_na_rm的摘要函数列表中添加了两个摘要函数(median_na_rmskim)。但是,默认情况下,这些新的摘要功能(在skimmers文档中称为skimr)显示在表格的末尾。相反,我希望medianiqr出现在meansd之后。

最终目标是在.Rmd报告中显示结果,如下所示:

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

```

渲染的HTML:

enter image description here

如您所见,在迷你图直方图之后,将打印medianiqr并在表格的末尾。我希望在sd之后和p0之前打印它们。有可能吗?

2 个答案:

答案 0 :(得分:3)

skim()输出中有两个部分。如果要控制数字部分,可以像这样使用skim_to_list。以另一种格式导出也更容易。

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   ▃▃▁▁▃▇▁▇

编辑

根据评论中的要求添加kable()

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 | ▃▃▁▁▃▇▁▇ |

答案 1 :(得分:1)

这是另一个使用append = FALSE选项的选项。

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)

my_skimmers <- list(n = length, missing = n_missing, complete = n_complete,
                     mean = mean.default, sd = purrr::partial(sd, na.rm = TRUE),
                     median = median_na_rm,  iqr = iqr_na_rm
                    )

skim_with(numeric = my_skimmers,
     integer = my_skimmers, append = FALSE)

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

我并没有输入所有统计信息,但是您可以查看function.R和stats.R文件,以了解如何定义各种统计信息。