Debian Codegraph @

R/skim.R

raw
1 #' Skim a data frame, getting useful summary statistics
2 #'
3 #' `skim()` is an alternative to [summary()], quickly providing a broad
4 #' overview of a data frame. It handles data of all types, dispatching a
5 #' different set of summary functions based on the types of columns in the data
6 #' frame.
7 #'
8 #' Each call produces a `skim_df`, which is a fundamentally a tibble with a
9 #' special print method. One unusual feature of this data frame is pseudo-
10 #' namespace for columns. `skim()` computes statistics by data type, and it
11 #' stores them in the data frame as `<type>.<statistic>`. These types are
12 #' stripped when printing the results. The "base" skimmers (`n_missing` and
13 #' `complete_rate`) are the only columns that don't follow this behavior.
14 #' See [skim_with()] for more details on customizing `skim()` and
15 #' [get_default_skimmers()] for a list of default functions.
16 #'
17 #' If you just want to see the printed output, call `skim_tee()` instead.
18 #' This function returns the original data. `skim_tee()` uses the default
19 #' `skim()`, but you can replace it with the `skim` argument.
20 #'
21 #' The data frame produced by `skim` is wide and sparse. To avoid type coercion
22 #' `skimr` uses a type namespace for all summary statistics. Columns for numeric
23 #' summary statistics all begin `numeric`; for factor summary statistics
24 #' begin `factor`; and so on.
25 #'
26 #' See [partition()] and [yank()] for methods for transforming this wide data
27 #' frame. The first function splits it into a list, with each entry
28 #' corresponding to a data type. The latter pulls a single subtable for a
29 #' particular type from the `skim_df`.
30 #'
31 #' `skim()` is designed to operate in pipes and to generally play nicely with
32 #' other `tidyverse` functions. This means that you can use `tidyselect` helpers
33 #' within `skim` to select or drop specific columns for summary. You can also
34 #' further work with a `skim_df` using `dplyr` functions in a pipeline.
35 #'
36 #' @section Customizing skim:
37 #' `skim()` is an intentionally simple function, with minimal arguments like
38 #' [summary()]. Nonetheless, this package provides two broad approaches to
39 #' how you can customize `skim()`'s behavior. You can customize the functions
40 #' that are called to produce summary statistics with [skim_with()].
41 #'
42 #' @section Unicode rendering:
43 #' If the rendered examples show unencoded values such as `<U+2587>` you will
44 #' need to change your locale to allow proper rendering. Please review the
45 #' *Using Skimr* vignette for more information
46 #' (`vignette("skimr", package = "skimr")`).
47 #'
48 #' Otherwise, we export `skim_without_charts()` to produce summaries without the
49 #' spark graphs. These are the source of the unicode dependency.
50 #'
51 #' @param data A tibble, or an object that can be coerced into a tibble.
52 #' @param ... Columns to select for skimming. When none are provided, the
53 #' default is to skim all columns.
54 #' @param .data_name The name to use for the data. Defaults to the same as data.
55 #' @param skim The skimming function to use in `skim_tee()`.
56 #' @return A `skim_df` object, which also inherits the class(es) of the input
57 #' data. In many ways, the object behaves like a [tibble::tibble()].
58 #' @examples
59 #' skim(iris)
60 #'
61 #' # Use tidyselect
62 #' skim(iris, Species)
63 #' skim(iris, starts_with("Sepal"))
64 #' skim(iris, where(is.numeric))
65 #'
66 #' # Skim also works groupwise
67 #' iris |>
68 #' dplyr::group_by(Species) |>
69 #' skim()
70 #'
71 #' # Which five numeric columns have the greatest mean value?
72 #' # Look in the `numeric.mean` column.
73 #' iris |>
74 #' skim() |>
75 #' dplyr::select(numeric.mean) |>
76 #' dplyr::slice_head(n = 5)
77 #'
78 #' # Which of my columns have missing values? Use the base skimmer n_missing.
79 #' iris |>
80 #' skim() |>
81 #' dplyr::filter(n_missing > 0)
82 #'
83 #' # Use skim_tee to view the skim results and
84 #' # continue using the original data.
85 #' chickwts |>
86 #' skim_tee() |>
87 #' dplyr::filter(feed == "sunflower")
88 #'
89 #' # Produce a summary without spark graphs
90 #' iris |>
91 #' skim_without_charts()
92 #' @export
93 skim <- skim_with()
94
95 #' @rdname skim
96 #' @param skim_fun The skim function used.
97 #' @export
98 skim_tee <- function(data, ..., skim_fun = skim) {
99 skimmed <- skim_fun(data, ...)
100 print(skimmed)
101 invisible(data)
102 }
103
104 #' @rdname skim
105 #' @export
106 skim_without_charts <- skim_with(
107 numeric = sfl(hist = NULL),
108 ts = sfl(line_graph = NULL)
109 )