Debian Codegraph @

inst/doc/extending_skimr.html

raw symbols SCIP
1 <!DOCTYPE html>
2
3 <html>
4
5 <head>
6
7 <meta charset="utf-8" />
8 <meta name="generator" content="pandoc" />
9 <meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
10
11 <meta name="viewport" content="width=device-width, initial-scale=1" />
12
13
14 <meta name="date" content="2026-01-09" />
15
16 <title>Extending skimr</title>
17
18 <script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to
19 // be compatible with the behavior of Pandoc < 2.8).
20 document.addEventListener('DOMContentLoaded', function(e) {
21 var hs = document.querySelectorAll("div.section[class*='level'] > :first-child");
22 var i, h, a;
23 for (i = 0; i < hs.length; i++) {
24 h = hs[i];
25 if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6
26 a = h.attributes;
27 while (a.length > 0) h.removeAttribute(a[0].name);
28 }
29 });
30 </script>
31
32 <style type="text/css">
33 code{white-space: pre-wrap;}
34 span.smallcaps{font-variant: small-caps;}
35 span.underline{text-decoration: underline;}
36 div.column{display: inline-block; vertical-align: top; width: 50%;}
37 div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
38 ul.task-list{list-style: none;}
39 </style>
40
41
42
43 <style type="text/css">
44 code {
45 white-space: pre;
46 }
47 .sourceCode {
48 overflow: visible;
49 }
50 </style>
51 <style type="text/css" data-origin="pandoc">
52 pre > code.sourceCode { white-space: pre; position: relative; }
53 pre > code.sourceCode > span { line-height: 1.25; }
54 pre > code.sourceCode > span:empty { height: 1.2em; }
55 .sourceCode { overflow: visible; }
56 code.sourceCode > span { color: inherit; text-decoration: inherit; }
57 div.sourceCode { margin: 1em 0; }
58 pre.sourceCode { margin: 0; }
59 @media screen {
60 div.sourceCode { overflow: auto; }
61 }
62 @media print {
63 pre > code.sourceCode { white-space: pre-wrap; }
64 pre > code.sourceCode > span { display: inline-block; text-indent: -5em; padding-left: 5em; }
65 }
66 pre.numberSource code
67 { counter-reset: source-line 0; }
68 pre.numberSource code > span
69 { position: relative; left: -4em; counter-increment: source-line; }
70 pre.numberSource code > span > a:first-child::before
71 { content: counter(source-line);
72 position: relative; left: -1em; text-align: right; vertical-align: baseline;
73 border: none; display: inline-block;
74 -webkit-touch-callout: none; -webkit-user-select: none;
75 -khtml-user-select: none; -moz-user-select: none;
76 -ms-user-select: none; user-select: none;
77 padding: 0 4px; width: 4em;
78 color: #aaaaaa;
79 }
80 pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; }
81 div.sourceCode
82 { }
83 @media screen {
84 pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
85 }
86 code span.al { color: #ff0000; font-weight: bold; }
87 code span.an { color: #60a0b0; font-weight: bold; font-style: italic; }
88 code span.at { color: #7d9029; }
89 code span.bn { color: #40a070; }
90 code span.bu { color: #008000; }
91 code span.cf { color: #007020; font-weight: bold; }
92 code span.ch { color: #4070a0; }
93 code span.cn { color: #880000; }
94 code span.co { color: #60a0b0; font-style: italic; }
95 code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; }
96 code span.do { color: #ba2121; font-style: italic; }
97 code span.dt { color: #902000; }
98 code span.dv { color: #40a070; }
99 code span.er { color: #ff0000; font-weight: bold; }
100 code span.ex { }
101 code span.fl { color: #40a070; }
102 code span.fu { color: #06287e; }
103 code span.im { color: #008000; font-weight: bold; }
104 code span.in { color: #60a0b0; font-weight: bold; font-style: italic; }
105 code span.kw { color: #007020; font-weight: bold; }
106 code span.op { color: #666666; }
107 code span.ot { color: #007020; }
108 code span.pp { color: #bc7a00; }
109 code span.sc { color: #4070a0; }
110 code span.ss { color: #bb6688; }
111 code span.st { color: #4070a0; }
112 code span.va { color: #19177c; }
113 code span.vs { color: #4070a0; }
114 code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; }
115 </style>
116 <script>
117 // apply pandoc div.sourceCode style to pre.sourceCode instead
118 (function() {
119 var sheets = document.styleSheets;
120 for (var i = 0; i < sheets.length; i++) {
121 if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
122 try { var rules = sheets[i].cssRules; } catch (e) { continue; }
123 var j = 0;
124 while (j < rules.length) {
125 var rule = rules[j];
126 // check if there is a div.sourceCode rule
127 if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") {
128 j++;
129 continue;
130 }
131 var style = rule.style.cssText;
132 // check if color or background-color is set
133 if (rule.style.color === '' && rule.style.backgroundColor === '') {
134 j++;
135 continue;
136 }
137 // replace div.sourceCode by a pre.sourceCode rule
138 sheets[i].deleteRule(j);
139 sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
140 }
141 }
142 })();
143 </script>
144
145
146
147
148 <style type="text/css">body {
149 background-color: #fff;
150 margin: 1em auto;
151 max-width: 700px;
152 overflow: visible;
153 padding-left: 2em;
154 padding-right: 2em;
155 font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
156 font-size: 14px;
157 line-height: 1.35;
158 }
159 #TOC {
160 clear: both;
161 margin: 0 0 10px 10px;
162 padding: 4px;
163 width: 400px;
164 border: 1px solid #CCCCCC;
165 border-radius: 5px;
166 background-color: #f6f6f6;
167 font-size: 13px;
168 line-height: 1.3;
169 }
170 #TOC .toctitle {
171 font-weight: bold;
172 font-size: 15px;
173 margin-left: 5px;
174 }
175 #TOC ul {
176 padding-left: 40px;
177 margin-left: -1.5em;
178 margin-top: 5px;
179 margin-bottom: 5px;
180 }
181 #TOC ul ul {
182 margin-left: -2em;
183 }
184 #TOC li {
185 line-height: 16px;
186 }
187 table {
188 margin: 1em auto;
189 border-width: 1px;
190 border-color: #DDDDDD;
191 border-style: outset;
192 border-collapse: collapse;
193 }
194 table th {
195 border-width: 2px;
196 padding: 5px;
197 border-style: inset;
198 }
199 table td {
200 border-width: 1px;
201 border-style: inset;
202 line-height: 18px;
203 padding: 5px 5px;
204 }
205 table, table th, table td {
206 border-left-style: none;
207 border-right-style: none;
208 }
209 table thead, table tr.even {
210 background-color: #f7f7f7;
211 }
212 p {
213 margin: 0.5em 0;
214 }
215 blockquote {
216 background-color: #f6f6f6;
217 padding: 0.25em 0.75em;
218 }
219 hr {
220 border-style: solid;
221 border: none;
222 border-top: 1px solid #777;
223 margin: 28px 0;
224 }
225 dl {
226 margin-left: 0;
227 }
228 dl dd {
229 margin-bottom: 13px;
230 margin-left: 13px;
231 }
232 dl dt {
233 font-weight: bold;
234 }
235 ul {
236 margin-top: 0;
237 }
238 ul li {
239 list-style: circle outside;
240 }
241 ul ul {
242 margin-bottom: 0;
243 }
244 pre, code {
245 background-color: #f7f7f7;
246 border-radius: 3px;
247 color: #333;
248 white-space: pre-wrap;
249 }
250 pre {
251 border-radius: 3px;
252 margin: 5px 0px 10px 0px;
253 padding: 10px;
254 }
255 pre:not([class]) {
256 background-color: #f7f7f7;
257 }
258 code {
259 font-family: Consolas, Monaco, 'Courier New', monospace;
260 font-size: 85%;
261 }
262 p > code, li > code {
263 padding: 2px 0px;
264 }
265 div.figure {
266 text-align: center;
267 }
268 img {
269 background-color: #FFFFFF;
270 padding: 2px;
271 border: 1px solid #DDDDDD;
272 border-radius: 3px;
273 border: 1px solid #CCCCCC;
274 margin: 0 5px;
275 }
276 h1 {
277 margin-top: 0;
278 font-size: 35px;
279 line-height: 40px;
280 }
281 h2 {
282 border-bottom: 4px solid #f7f7f7;
283 padding-top: 10px;
284 padding-bottom: 2px;
285 font-size: 145%;
286 }
287 h3 {
288 border-bottom: 2px solid #f7f7f7;
289 padding-top: 10px;
290 font-size: 120%;
291 }
292 h4 {
293 border-bottom: 1px solid #f7f7f7;
294 margin-left: 8px;
295 font-size: 105%;
296 }
297 h5, h6 {
298 border-bottom: 1px solid #ccc;
299 font-size: 105%;
300 }
301 a {
302 color: #0033dd;
303 text-decoration: none;
304 }
305 a:hover {
306 color: #6666ff; }
307 a:visited {
308 color: #800080; }
309 a:visited:hover {
310 color: #BB00BB; }
311 a[href^="http:"] {
312 text-decoration: underline; }
313 a[href^="https:"] {
314 text-decoration: underline; }
315
316 code > span.kw { color: #555; font-weight: bold; }
317 code > span.dt { color: #902000; }
318 code > span.dv { color: #40a070; }
319 code > span.bn { color: #d14; }
320 code > span.fl { color: #d14; }
321 code > span.ch { color: #d14; }
322 code > span.st { color: #d14; }
323 code > span.co { color: #888888; font-style: italic; }
324 code > span.ot { color: #007020; }
325 code > span.al { color: #ff0000; font-weight: bold; }
326 code > span.fu { color: #900; font-weight: bold; }
327 code > span.er { color: #a61717; background-color: #e3d2d2; }
328 </style>
329
330
331
332
333 </head>
334
335 <body>
336
337
338
339
340 <h1 class="title toc-ignore">Extending skimr</h1>
341 <h4 class="date">2026-01-09</h4>
342
343
344
345 <div id="introduction" class="section level2">
346 <h2>Introduction</h2>
347 <p>The <code>skim()</code> function summarizes data types contained
348 within data frames and objects that have <code>as.data.frame()</code>
349 methods to coerce them into data frames. It comes with a set of default
350 summary functions for a wide variety of data types, but this is not
351 comprehensive.</p>
352 <p>Package authors (and advanced users) can add support for skimming
353 their specific non-data-frame objects in their packages, and they can
354 provide different defaults in their own summary functions. This will
355 require including skimr as a dependency.</p>
356 </div>
357 <div id="skimming-objects-that-are-not-coercible-to-data-frames" class="section level1">
358 <h1>Skimming objects that are not coercible to data frames</h1>
359 <p>This example will illustrate this by creating support for the
360 <code>lm</code> object produced by <code>lm()</code>. For any object
361 this involves two required elements and one optional element. This is a
362 simple example, but for other types of objects there may be much more
363 complexity</p>
364 <p>If you are adding skim support to a package you will also need to add
365 <code>skimr</code> to the list of imports.</p>
366 <div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" tabindex="-1"></a><span class="fu">library</span>(skimr)</span></code></pre></div>
367 <p>The <code>lm()</code> function produces a complex object with class
368 “lm”.</p>
369 <div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>results <span class="ot">&lt;-</span> <span class="fu">lm</span>(weight <span class="sc">~</span> feed, <span class="at">data =</span> chickwts)</span>
370 <span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a><span class="fu">class</span>(results)</span></code></pre></div>
371 <pre><code>## [1] &quot;lm&quot;</code></pre>
372 <div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" tabindex="-1"></a><span class="fu">attributes</span>(results)</span></code></pre></div>
373 <pre><code>## $names
374 ## [1] &quot;coefficients&quot; &quot;residuals&quot; &quot;effects&quot; &quot;rank&quot;
375 ## [5] &quot;fitted.values&quot; &quot;assign&quot; &quot;qr&quot; &quot;df.residual&quot;
376 ## [9] &quot;contrasts&quot; &quot;xlevels&quot; &quot;call&quot; &quot;terms&quot;
377 ## [13] &quot;model&quot;
378 ##
379 ## $class
380 ## [1] &quot;lm&quot;</code></pre>
381 <p>There is no as.data.frame method for an <code>lm</code> object.</p>
382 <div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" tabindex="-1"></a><span class="fu">as.data.frame</span>(results)</span>
383 <span id="cb6-2"><a href="#cb6-2" tabindex="-1"></a><span class="co">#&gt; Error in as.data.frame.default(results) :</span></span>
384 <span id="cb6-3"><a href="#cb6-3" tabindex="-1"></a><span class="co">#&gt; cannot coerce class ‘&quot;lm&quot;’ to a data.frame</span></span></code></pre></div>
385 <p>Unlike the example of having a new type of data in a column of a
386 simple data frame (for which we would create a <code>sfl</code>) frame
387 in the “Using skimr” vignette, this is a different type of challenge: an
388 object that we might wish to skim, but that cannot be directly skimmed.
389 Therefore we need to make it into an object that is either a data frame
390 or coercible to a data frame.</p>
391 <p>In the case of the lm object, the <code>model</code> attribute is
392 already a data frame. So a very simple way to solve the challenge is to
393 skim <code>results$model</code> directly.</p>
394 <div class="sourceCode" id="cb7"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" tabindex="-1"></a><span class="fu">skim</span>(results<span class="sc">$</span>model)</span></code></pre></div>
395 <table>
396 <caption>Data summary</caption>
397 <tbody>
398 <tr class="odd">
399 <td align="left">Name</td>
400 <td align="left">results$model</td>
401 </tr>
402 <tr class="even">
403 <td align="left">Number of rows</td>
404 <td align="left">71</td>
405 </tr>
406 <tr class="odd">
407 <td align="left">Number of columns</td>
408 <td align="left">2</td>
409 </tr>
410 <tr class="even">
411 <td align="left">_______________________</td>
412 <td align="left"></td>
413 </tr>
414 <tr class="odd">
415 <td align="left">Column type frequency:</td>
416 <td align="left"></td>
417 </tr>
418 <tr class="even">
419 <td align="left">factor</td>
420 <td align="left">1</td>
421 </tr>
422 <tr class="odd">
423 <td align="left">numeric</td>
424 <td align="left">1</td>
425 </tr>
426 <tr class="even">
427 <td align="left">________________________</td>
428 <td align="left"></td>
429 </tr>
430 <tr class="odd">
431 <td align="left">Group variables</td>
432 <td align="left">None</td>
433 </tr>
434 </tbody>
435 </table>
436 <p><strong>Variable type: factor</strong></p>
437 <table>
438 <colgroup>
439 <col width="15%" />
440 <col width="11%" />
441 <col width="15%" />
442 <col width="8%" />
443 <col width="10%" />
444 <col width="38%" />
445 </colgroup>
446 <thead>
447 <tr class="header">
448 <th align="left">skim_variable</th>
449 <th align="right">n_missing</th>
450 <th align="right">complete_rate</th>
451 <th align="left">ordered</th>
452 <th align="right">n_unique</th>
453 <th align="left">top_counts</th>
454 </tr>
455 </thead>
456 <tbody>
457 <tr class="odd">
458 <td align="left">feed</td>
459 <td align="right">0</td>
460 <td align="right">1</td>
461 <td align="left">FALSE</td>
462 <td align="right">6</td>
463 <td align="left">soy: 14, cas: 12, lin: 12, sun: 12</td>
464 </tr>
465 </tbody>
466 </table>
467 <p><strong>Variable type: numeric</strong></p>
468 <table style="width:100%;">
469 <colgroup>
470 <col width="17%" />
471 <col width="12%" />
472 <col width="17%" />
473 <col width="8%" />
474 <col width="7%" />
475 <col width="4%" />
476 <col width="7%" />
477 <col width="4%" />
478 <col width="7%" />
479 <col width="6%" />
480 <col width="7%" />
481 </colgroup>
482 <thead>
483 <tr class="header">
484 <th align="left">skim_variable</th>
485 <th align="right">n_missing</th>
486 <th align="right">complete_rate</th>
487 <th align="right">mean</th>
488 <th align="right">sd</th>
489 <th align="right">p0</th>
490 <th align="right">p25</th>
491 <th align="right">p50</th>
492 <th align="right">p75</th>
493 <th align="right">p100</th>
494 <th align="left">hist</th>
495 </tr>
496 </thead>
497 <tbody>
498 <tr class="odd">
499 <td align="left">weight</td>
500 <td align="right">0</td>
501 <td align="right">1</td>
502 <td align="right">261.31</td>
503 <td align="right">78.07</td>
504 <td align="right">108</td>
505 <td align="right">204.5</td>
506 <td align="right">258</td>
507 <td align="right">323.5</td>
508 <td align="right">423</td>
509 <td align="left">▆▆▇▇▃</td>
510 </tr>
511 </tbody>
512 </table>
513 <p>This is works, but we could go one step further and create a new
514 function for doing this directly.</p>
515 <div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb8-1"><a href="#cb8-1" tabindex="-1"></a>skim_lm <span class="ot">&lt;-</span> <span class="cf">function</span>(.data) {</span>
516 <span id="cb8-2"><a href="#cb8-2" tabindex="-1"></a> .data <span class="ot">&lt;-</span> .data<span class="sc">$</span>model</span>
517 <span id="cb8-3"><a href="#cb8-3" tabindex="-1"></a> skimr<span class="sc">::</span><span class="fu">skim</span>(.data)</span>
518 <span id="cb8-4"><a href="#cb8-4" tabindex="-1"></a>}</span>
519 <span id="cb8-5"><a href="#cb8-5" tabindex="-1"></a></span>
520 <span id="cb8-6"><a href="#cb8-6" tabindex="-1"></a><span class="fu">lm</span>(weight <span class="sc">~</span> feed, <span class="at">data =</span> chickwts) <span class="sc">|&gt;</span> <span class="fu">skim_lm</span>()</span></code></pre></div>
521 <table>
522 <caption>Data summary</caption>
523 <tbody>
524 <tr class="odd">
525 <td align="left">Name</td>
526 <td align="left">Piped data</td>
527 </tr>
528 <tr class="even">
529 <td align="left">Number of rows</td>
530 <td align="left">71</td>
531 </tr>
532 <tr class="odd">
533 <td align="left">Number of columns</td>
534 <td align="left">2</td>
535 </tr>
536 <tr class="even">
537 <td align="left">_______________________</td>
538 <td align="left"></td>
539 </tr>
540 <tr class="odd">
541 <td align="left">Column type frequency:</td>
542 <td align="left"></td>
543 </tr>
544 <tr class="even">
545 <td align="left">factor</td>
546 <td align="left">1</td>
547 </tr>
548 <tr class="odd">
549 <td align="left">numeric</td>
550 <td align="left">1</td>
551 </tr>
552 <tr class="even">
553 <td align="left">________________________</td>
554 <td align="left"></td>
555 </tr>
556 <tr class="odd">
557 <td align="left">Group variables</td>
558 <td align="left">None</td>
559 </tr>
560 </tbody>
561 </table>
562 <p><strong>Variable type: factor</strong></p>
563 <table>
564 <colgroup>
565 <col width="15%" />
566 <col width="11%" />
567 <col width="15%" />
568 <col width="8%" />
569 <col width="10%" />
570 <col width="38%" />
571 </colgroup>
572 <thead>
573 <tr class="header">
574 <th align="left">skim_variable</th>
575 <th align="right">n_missing</th>
576 <th align="right">complete_rate</th>
577 <th align="left">ordered</th>
578 <th align="right">n_unique</th>
579 <th align="left">top_counts</th>
580 </tr>
581 </thead>
582 <tbody>
583 <tr class="odd">
584 <td align="left">feed</td>
585 <td align="right">0</td>
586 <td align="right">1</td>
587 <td align="left">FALSE</td>
588 <td align="right">6</td>
589 <td align="left">soy: 14, cas: 12, lin: 12, sun: 12</td>
590 </tr>
591 </tbody>
592 </table>
593 <p><strong>Variable type: numeric</strong></p>
594 <table style="width:100%;">
595 <colgroup>
596 <col width="17%" />
597 <col width="12%" />
598 <col width="17%" />
599 <col width="8%" />
600 <col width="7%" />
601 <col width="4%" />
602 <col width="7%" />
603 <col width="4%" />
604 <col width="7%" />
605 <col width="6%" />
606 <col width="7%" />
607 </colgroup>
608 <thead>
609 <tr class="header">
610 <th align="left">skim_variable</th>
611 <th align="right">n_missing</th>
612 <th align="right">complete_rate</th>
613 <th align="right">mean</th>
614 <th align="right">sd</th>
615 <th align="right">p0</th>
616 <th align="right">p25</th>
617 <th align="right">p50</th>
618 <th align="right">p75</th>
619 <th align="right">p100</th>
620 <th align="left">hist</th>
621 </tr>
622 </thead>
623 <tbody>
624 <tr class="odd">
625 <td align="left">weight</td>
626 <td align="right">0</td>
627 <td align="right">1</td>
628 <td align="right">261.31</td>
629 <td align="right">78.07</td>
630 <td align="right">108</td>
631 <td align="right">204.5</td>
632 <td align="right">258</td>
633 <td align="right">323.5</td>
634 <td align="right">423</td>
635 <td align="left">▆▆▇▇▃</td>
636 </tr>
637 </tbody>
638 </table>
639 <p>If desired, a more complex function can be created. For example, the
640 lm object also contains fitted values and residuals. We could
641 incorporate these in the data frame.</p>
642 <div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a>skim_lm <span class="ot">&lt;-</span> <span class="cf">function</span>(.data, <span class="at">fit =</span> <span class="cn">FALSE</span>) {</span>
643 <span id="cb9-2"><a href="#cb9-2" tabindex="-1"></a> .data <span class="ot">&lt;-</span> .data<span class="sc">$</span>model</span>
644 <span id="cb9-3"><a href="#cb9-3" tabindex="-1"></a> <span class="cf">if</span> (fit) {</span>
645 <span id="cb9-4"><a href="#cb9-4" tabindex="-1"></a> .data <span class="ot">&lt;-</span> .data <span class="sc">|&gt;</span></span>
646 <span id="cb9-5"><a href="#cb9-5" tabindex="-1"></a> dplyr<span class="sc">::</span><span class="fu">bind_cols</span>(</span>
647 <span id="cb9-6"><a href="#cb9-6" tabindex="-1"></a> <span class="at">fitted =</span> <span class="fu">data.frame</span>(results<span class="sc">$</span>fitted.values),</span>
648 <span id="cb9-7"><a href="#cb9-7" tabindex="-1"></a> <span class="at">residuals =</span> <span class="fu">data.frame</span>(results<span class="sc">$</span>residuals)</span>
649 <span id="cb9-8"><a href="#cb9-8" tabindex="-1"></a> )</span>
650 <span id="cb9-9"><a href="#cb9-9" tabindex="-1"></a> }</span>
651 <span id="cb9-10"><a href="#cb9-10" tabindex="-1"></a> skimr<span class="sc">::</span><span class="fu">skim</span>(.data)</span>
652 <span id="cb9-11"><a href="#cb9-11" tabindex="-1"></a>}</span></code></pre></div>
653 <div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" tabindex="-1"></a><span class="fu">skim_lm</span>(results, <span class="at">fit =</span> <span class="cn">TRUE</span>)</span></code></pre></div>
654 <table>
655 <caption>Data summary</caption>
656 <tbody>
657 <tr class="odd">
658 <td align="left">Name</td>
659 <td align="left">Piped data</td>
660 </tr>
661 <tr class="even">
662 <td align="left">Number of rows</td>
663 <td align="left">71</td>
664 </tr>
665 <tr class="odd">
666 <td align="left">Number of columns</td>
667 <td align="left">4</td>
668 </tr>
669 <tr class="even">
670 <td align="left">_______________________</td>
671 <td align="left"></td>
672 </tr>
673 <tr class="odd">
674 <td align="left">Column type frequency:</td>
675 <td align="left"></td>
676 </tr>
677 <tr class="even">
678 <td align="left">factor</td>
679 <td align="left">1</td>
680 </tr>
681 <tr class="odd">
682 <td align="left">numeric</td>
683 <td align="left">3</td>
684 </tr>
685 <tr class="even">
686 <td align="left">________________________</td>
687 <td align="left"></td>
688 </tr>
689 <tr class="odd">
690 <td align="left">Group variables</td>
691 <td align="left">None</td>
692 </tr>
693 </tbody>
694 </table>
695 <p><strong>Variable type: factor</strong></p>
696 <table>
697 <colgroup>
698 <col width="15%" />
699 <col width="11%" />
700 <col width="15%" />
701 <col width="8%" />
702 <col width="10%" />
703 <col width="38%" />
704 </colgroup>
705 <thead>
706 <tr class="header">
707 <th align="left">skim_variable</th>
708 <th align="right">n_missing</th>
709 <th align="right">complete_rate</th>
710 <th align="left">ordered</th>
711 <th align="right">n_unique</th>
712 <th align="left">top_counts</th>
713 </tr>
714 </thead>
715 <tbody>
716 <tr class="odd">
717 <td align="left">feed</td>
718 <td align="right">0</td>
719 <td align="right">1</td>
720 <td align="left">FALSE</td>
721 <td align="right">6</td>
722 <td align="left">soy: 14, cas: 12, lin: 12, sun: 12</td>
723 </tr>
724 </tbody>
725 </table>
726 <p><strong>Variable type: numeric</strong></p>
727 <table style="width:100%;">
728 <colgroup>
729 <col width="21%" />
730 <col width="9%" />
731 <col width="13%" />
732 <col width="6%" />
733 <col width="5%" />
734 <col width="7%" />
735 <col width="6%" />
736 <col width="6%" />
737 <col width="6%" />
738 <col width="6%" />
739 <col width="5%" />
740 </colgroup>
741 <thead>
742 <tr class="header">
743 <th align="left">skim_variable</th>
744 <th align="right">n_missing</th>
745 <th align="right">complete_rate</th>
746 <th align="right">mean</th>
747 <th align="right">sd</th>
748 <th align="right">p0</th>
749 <th align="right">p25</th>
750 <th align="right">p50</th>
751 <th align="right">p75</th>
752 <th align="right">p100</th>
753 <th align="left">hist</th>
754 </tr>
755 </thead>
756 <tbody>
757 <tr class="odd">
758 <td align="left">weight</td>
759 <td align="right">0</td>
760 <td align="right">1</td>
761 <td align="right">261.31</td>
762 <td align="right">78.07</td>
763 <td align="right">108.00</td>
764 <td align="right">204.50</td>
765 <td align="right">258.00</td>
766 <td align="right">323.50</td>
767 <td align="right">423.00</td>
768 <td align="left">▆▆▇▇▃</td>
769 </tr>
770 <tr class="even">
771 <td align="left">results.fitted.values</td>
772 <td align="right">0</td>
773 <td align="right">1</td>
774 <td align="right">261.31</td>
775 <td align="right">57.46</td>
776 <td align="right">160.20</td>
777 <td align="right">218.75</td>
778 <td align="right">246.43</td>
779 <td align="right">323.58</td>
780 <td align="right">328.92</td>
781 <td align="left">▃▃▅▃▇</td>
782 </tr>
783 <tr class="odd">
784 <td align="left">results.residuals</td>
785 <td align="right">0</td>
786 <td align="right">1</td>
787 <td align="right">0.00</td>
788 <td align="right">52.86</td>
789 <td align="right">-123.91</td>
790 <td align="right">-34.41</td>
791 <td align="right">1.57</td>
792 <td align="right">38.17</td>
793 <td align="right">103.09</td>
794 <td align="left">▂▅▇▅▃</td>
795 </tr>
796 </tbody>
797 </table>
798 <p>A second example of the need for a special function is with
799 <code>dist</code> objects. The <code>UScitiesD</code> data set is an
800 example of this.</p>
801 <div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a><span class="fu">class</span>(UScitiesD)</span></code></pre></div>
802 <pre><code>## [1] &quot;dist&quot;</code></pre>
803 <div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>UScitiesD</span></code></pre></div>
804 <pre><code>## Atlanta Chicago Denver Houston LosAngeles Miami NewYork
805 ## Chicago 587
806 ## Denver 1212 920
807 ## Houston 701 940 879
808 ## LosAngeles 1936 1745 831 1374
809 ## Miami 604 1188 1726 968 2339
810 ## NewYork 748 713 1631 1420 2451 1092
811 ## SanFrancisco 2139 1858 949 1645 347 2594 2571
812 ## Seattle 2182 1737 1021 1891 959 2734 2408
813 ## Washington.DC 543 597 1494 1220 2300 923 205
814 ## SanFrancisco Seattle
815 ## Chicago
816 ## Denver
817 ## Houston
818 ## LosAngeles
819 ## Miami
820 ## NewYork
821 ## SanFrancisco
822 ## Seattle 678
823 ## Washington.DC 2442 2329</code></pre>
824 <p>A <code>dist</code> object is most often, as in this case, lower
825 triange matrices of distances, which can be measured in various ways.
826 There are many packages that produce dist objects and/or take dist
827 objects as inputs, including those for cluster analysis and
828 multidimensional scaling.</p>
829 <p>A simple solution to this is to follow a similar design to that for
830 <code>lm</code> objects.</p>
831 <div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>skim_dist <span class="ot">&lt;-</span> <span class="cf">function</span>(.data) {</span>
832 <span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> .data <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(<span class="fu">as.matrix</span>(.data))</span>
833 <span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> skimr<span class="sc">::</span><span class="fu">skim</span>(.data)</span>
834 <span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a>}</span></code></pre></div>
835 <p>However, this has the limitation of treating the dist data as though
836 it is simple numeric data.</p>
837 <p>What we might want to do, instead, is to create a new class, for
838 example, “distance” that is specifically for distance data. This will
839 allow it to have its own <code>sfl</code> or skimr function list.</p>
840 <p>As handling gets more complex, rather than make a new function it can
841 be more powerful to define an <code>as.data.frame</code> S3 method for
842 dist objects, which will allow it to integrate with skimr more
843 completely and uses to use the <code>skim()</code> function directly. In
844 a package you will want to export this.</p>
845 <div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb16-1"><a href="#cb16-1" tabindex="-1"></a>as.data.frame.dist <span class="ot">&lt;-</span> <span class="cf">function</span>(.data) {</span>
846 <span id="cb16-2"><a href="#cb16-2" tabindex="-1"></a> .data <span class="ot">&lt;-</span> <span class="fu">data.frame</span>(<span class="fu">as.matrix</span>(.data))</span>
847 <span id="cb16-3"><a href="#cb16-3" tabindex="-1"></a></span>
848 <span id="cb16-4"><a href="#cb16-4" tabindex="-1"></a> .data[] <span class="ot">&lt;-</span> <span class="fu">lapply</span>(.data, structure, <span class="at">class =</span> <span class="st">&quot;distance&quot;</span>, <span class="at">nms =</span> <span class="fu">names</span>(.data))</span>
849 <span id="cb16-5"><a href="#cb16-5" tabindex="-1"></a> .data</span>
850 <span id="cb16-6"><a href="#cb16-6" tabindex="-1"></a>}</span>
851 <span id="cb16-7"><a href="#cb16-7" tabindex="-1"></a></span>
852 <span id="cb16-8"><a href="#cb16-8" tabindex="-1"></a><span class="fu">as.data.frame</span>(UScitiesD)</span></code></pre></div>
853 <pre><code>## Atlanta Chicago Denver Houston LosAngeles Miami NewYork
854 ## Atlanta 0 587 1212 701 1936 604 748
855 ## Chicago 587 0 920 940 1745 1188 713
856 ## Denver 1212 920 0 879 831 1726 1631
857 ## Houston 701 940 879 0 1374 968 1420
858 ## LosAngeles 1936 1745 831 1374 0 2339 2451
859 ## Miami 604 1188 1726 968 2339 0 1092
860 ## NewYork 748 713 1631 1420 2451 1092 0
861 ## SanFrancisco 2139 1858 949 1645 347 2594 2571
862 ## Seattle 2182 1737 1021 1891 959 2734 2408
863 ## Washington.DC 543 597 1494 1220 2300 923 205
864 ## SanFrancisco Seattle Washington.DC
865 ## Atlanta 2139 2182 543
866 ## Chicago 1858 1737 597
867 ## Denver 949 1021 1494
868 ## Houston 1645 1891 1220
869 ## LosAngeles 347 959 2300
870 ## Miami 2594 2734 923
871 ## NewYork 2571 2408 205
872 ## SanFrancisco 0 678 2442
873 ## Seattle 678 0 2329
874 ## Washington.DC 2442 2329 0</code></pre>
875 <p>However, until an <code>sfl</code> is created, <code>skimr</code>
876 will not recognize the class and fall back to treating the data as if it
877 were character data.</p>
878 <div class="sourceCode" id="cb18"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" tabindex="-1"></a><span class="fu">skim</span>(UScitiesD)</span></code></pre></div>
879 <pre><code>## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
880 ## provided. Falling back to `character`.
881 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
882 ## provided. Falling back to `character`.
883 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
884 ## provided. Falling back to `character`.
885 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
886 ## provided. Falling back to `character`.
887 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
888 ## provided. Falling back to `character`.
889 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
890 ## provided. Falling back to `character`.
891 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
892 ## provided. Falling back to `character`.
893 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
894 ## provided. Falling back to `character`.
895 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
896 ## provided. Falling back to `character`.
897 ## Warning: Couldn&#39;t find skimmers for class: distance; No user-defined `sfl`
898 ## provided. Falling back to `character`.</code></pre>
899 <table>
900 <caption>Data summary</caption>
901 <tbody>
902 <tr class="odd">
903 <td align="left">Name</td>
904 <td align="left">UScitiesD</td>
905 </tr>
906 <tr class="even">
907 <td align="left">Number of rows</td>
908 <td align="left">10</td>
909 </tr>
910 <tr class="odd">
911 <td align="left">Number of columns</td>
912 <td align="left">10</td>
913 </tr>
914 <tr class="even">
915 <td align="left">_______________________</td>
916 <td align="left"></td>
917 </tr>
918 <tr class="odd">
919 <td align="left">Column type frequency:</td>
920 <td align="left"></td>
921 </tr>
922 <tr class="even">
923 <td align="left">character</td>
924 <td align="left">10</td>
925 </tr>
926 <tr class="odd">
927 <td align="left">________________________</td>
928 <td align="left"></td>
929 </tr>
930 <tr class="even">
931 <td align="left">Group variables</td>
932 <td align="left">None</td>
933 </tr>
934 </tbody>
935 </table>
936 <p><strong>Variable type: character</strong></p>
937 <table>
938 <colgroup>
939 <col width="19%" />
940 <col width="13%" />
941 <col width="19%" />
942 <col width="5%" />
943 <col width="5%" />
944 <col width="8%" />
945 <col width="12%" />
946 <col width="15%" />
947 </colgroup>
948 <thead>
949 <tr class="header">
950 <th align="left">skim_variable</th>
951 <th align="right">n_missing</th>
952 <th align="right">complete_rate</th>
953 <th align="right">min</th>
954 <th align="right">max</th>
955 <th align="right">empty</th>
956 <th align="right">n_unique</th>
957 <th align="right">whitespace</th>
958 </tr>
959 </thead>
960 <tbody>
961 <tr class="odd">
962 <td align="left">Atlanta</td>
963 <td align="right">0</td>
964 <td align="right">1</td>
965 <td align="right">1</td>
966 <td align="right">4</td>
967 <td align="right">0</td>
968 <td align="right">10</td>
969 <td align="right">0</td>
970 </tr>
971 <tr class="even">
972 <td align="left">Chicago</td>
973 <td align="right">0</td>
974 <td align="right">1</td>
975 <td align="right">1</td>
976 <td align="right">4</td>
977 <td align="right">0</td>
978 <td align="right">10</td>
979 <td align="right">0</td>
980 </tr>
981 <tr class="odd">
982 <td align="left">Denver</td>
983 <td align="right">0</td>
984 <td align="right">1</td>
985 <td align="right">1</td>
986 <td align="right">4</td>
987 <td align="right">0</td>
988 <td align="right">10</td>
989 <td align="right">0</td>
990 </tr>
991 <tr class="even">
992 <td align="left">Houston</td>
993 <td align="right">0</td>
994 <td align="right">1</td>
995 <td align="right">1</td>
996 <td align="right">4</td>
997 <td align="right">0</td>
998 <td align="right">10</td>
999 <td align="right">0</td>
1000 </tr>
1001 <tr class="odd">
1002 <td align="left">LosAngeles</td>
1003 <td align="right">0</td>
1004 <td align="right">1</td>
1005 <td align="right">1</td>
1006 <td align="right">4</td>
1007 <td align="right">0</td>
1008 <td align="right">10</td>
1009 <td align="right">0</td>
1010 </tr>
1011 <tr class="even">
1012 <td align="left">Miami</td>
1013 <td align="right">0</td>
1014 <td align="right">1</td>
1015 <td align="right">1</td>
1016 <td align="right">4</td>
1017 <td align="right">0</td>
1018 <td align="right">10</td>
1019 <td align="right">0</td>
1020 </tr>
1021 <tr class="odd">
1022 <td align="left">NewYork</td>
1023 <td align="right">0</td>
1024 <td align="right">1</td>
1025 <td align="right">1</td>
1026 <td align="right">4</td>
1027 <td align="right">0</td>
1028 <td align="right">10</td>
1029 <td align="right">0</td>
1030 </tr>
1031 <tr class="even">
1032 <td align="left">SanFrancisco</td>
1033 <td align="right">0</td>
1034 <td align="right">1</td>
1035 <td align="right">1</td>
1036 <td align="right">4</td>
1037 <td align="right">0</td>
1038 <td align="right">10</td>
1039 <td align="right">0</td>
1040 </tr>
1041 <tr class="odd">
1042 <td align="left">Seattle</td>
1043 <td align="right">0</td>
1044 <td align="right">1</td>
1045 <td align="right">1</td>
1046 <td align="right">4</td>
1047 <td align="right">0</td>
1048 <td align="right">10</td>
1049 <td align="right">0</td>
1050 </tr>
1051 <tr class="even">
1052 <td align="left">Washington.DC</td>
1053 <td align="right">0</td>
1054 <td align="right">1</td>
1055 <td align="right">1</td>
1056 <td align="right">4</td>
1057 <td align="right">0</td>
1058 <td align="right">10</td>
1059 <td align="right">0</td>
1060 </tr>
1061 </tbody>
1062 </table>
1063 <p>The solution to this is to define an <code>sfl</code> (skimr function
1064 list) specifically for the <code>distance</code> class.</p>
1065 <div id="defining-sfls-for-a-package" class="section level2">
1066 <h2>Defining sfl’s for a package</h2>
1067 <p><code>skimr</code> has an opinionated list of functions for each
1068 class (e.g. numeric, factor) of data. The core package supports many
1069 commonly used classes, but there are many others. You can investigate
1070 these defaults by calling <code>get_default_skimmer_names()</code>.</p>
1071 <p>What if your data type, like <code>distance</code>, isn’t covered by
1072 defaults? <code>skimr</code> usually falls back to treating the type as
1073 a character, which isn’t necessarily helpful. In this case, you’re best
1074 off adding your data type with <code>skim_with()</code>.</p>
1075 <p>Before we begin, we’ll be using the following custom summary
1076 statistics throughout. These functions find the nearest and furthest
1077 other location for each location.</p>
1078 <p>One thing that is important to be aware of when creating statistics
1079 functions for skimr is that skimr largely uses tibbles rather than base
1080 data frames. This means that many base operations do not work as
1081 expected.</p>
1082 <div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" tabindex="-1"></a>get_nearest <span class="ot">&lt;-</span> <span class="cf">function</span>(column) {</span>
1083 <span id="cb20-2"><a href="#cb20-2" tabindex="-1"></a> closest <span class="ot">&lt;-</span> <span class="fu">which.min</span>(column[column <span class="sc">!=</span> <span class="dv">0</span>])</span>
1084 <span id="cb20-3"><a href="#cb20-3" tabindex="-1"></a> cities <span class="ot">&lt;-</span> <span class="fu">attr</span>(column, <span class="st">&quot;nms&quot;</span>)[column <span class="sc">!=</span> <span class="dv">0</span>]</span>
1085 <span id="cb20-4"><a href="#cb20-4" tabindex="-1"></a> <span class="fu">toString</span>(cities[closest])</span>
1086 <span id="cb20-5"><a href="#cb20-5" tabindex="-1"></a>}</span>
1087 <span id="cb20-6"><a href="#cb20-6" tabindex="-1"></a></span>
1088 <span id="cb20-7"><a href="#cb20-7" tabindex="-1"></a>get_furthest <span class="ot">&lt;-</span> <span class="cf">function</span>(column) {</span>
1089 <span id="cb20-8"><a href="#cb20-8" tabindex="-1"></a> furthest <span class="ot">&lt;-</span> <span class="fu">which.max</span>(column[column <span class="sc">!=</span> <span class="dv">0</span>])</span>
1090 <span id="cb20-9"><a href="#cb20-9" tabindex="-1"></a> cities <span class="ot">&lt;-</span> <span class="fu">attr</span>(column, <span class="st">&quot;nms&quot;</span>)[column <span class="sc">!=</span> <span class="dv">0</span>]</span>
1091 <span id="cb20-10"><a href="#cb20-10" tabindex="-1"></a> <span class="fu">toString</span>(cities[furthest])</span>
1092 <span id="cb20-11"><a href="#cb20-11" tabindex="-1"></a>}</span></code></pre></div>
1093 <p>This function, like all summary functions used by <code>skimr</code>
1094 has two notable features.</p>
1095 <ul>
1096 <li>It accepts a vector as its single argument</li>
1097 <li>it returns a scalar, or in R terminology, a vector of length 1.</li>
1098 </ul>
1099 <p>There are a lot of functions that fulfill these criteria:</p>
1100 <ul>
1101 <li>Existing functions from base, stats, or other packages,</li>
1102 <li>lambda’s created using the Tidyverse-style syntax</li>
1103 <li>custom functions that have been defined in the <code>skimr</code>
1104 package</li>
1105 <li>custom functions that you have defined.</li>
1106 </ul>
1107 <p>Not fulfilling the two criteria can lead to some very confusing
1108 behavior within <code>skimr</code>. Beware! An example of this issue is
1109 the base <code>quantile()</code> function in default <code>skimr</code>
1110 percentiles are returned by using <code>quantile()</code> five times. In
1111 the case of these functions, there could be ties which would result in
1112 returning vectors that have length greater than 1. This is handled by
1113 collapsing all of the tied values into a single string.</p>
1114 <p>Notice, also, that in the case of distance data we may wish to
1115 exclude distances of 0, which indicate the distance from a place to
1116 itself. In finding the minimum our function looks only at the distance
1117 to other places.</p>
1118 <p>There are at least two ways that you might want to customize skimr
1119 handling of a special data type within a package or your own work. The
1120 first is to create a custom skimming function.</p>
1121 <div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a>skim_with_dist <span class="ot">&lt;-</span> <span class="fu">skim_with</span>(</span>
1122 <span id="cb21-2"><a href="#cb21-2" tabindex="-1"></a> <span class="at">distance =</span> <span class="fu">sfl</span>(</span>
1123 <span id="cb21-3"><a href="#cb21-3" tabindex="-1"></a> <span class="at">nearest =</span> get_nearest,</span>
1124 <span id="cb21-4"><a href="#cb21-4" tabindex="-1"></a> <span class="at">furthest =</span> get_furthest</span>
1125 <span id="cb21-5"><a href="#cb21-5" tabindex="-1"></a> )</span>
1126 <span id="cb21-6"><a href="#cb21-6" tabindex="-1"></a>)</span></code></pre></div>
1127 <pre><code>## Creating new skimming functions for the following classes: distance.
1128 ## They did not have recognized defaults. Call get_default_skimmers() for more information.</code></pre>
1129 <div class="sourceCode" id="cb23"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" tabindex="-1"></a><span class="fu">skim_with_dist</span>(UScitiesD)</span></code></pre></div>
1130 <table>
1131 <caption>Data summary</caption>
1132 <tbody>
1133 <tr class="odd">
1134 <td align="left">Name</td>
1135 <td align="left">UScitiesD</td>
1136 </tr>
1137 <tr class="even">
1138 <td align="left">Number of rows</td>
1139 <td align="left">10</td>
1140 </tr>
1141 <tr class="odd">
1142 <td align="left">Number of columns</td>
1143 <td align="left">10</td>
1144 </tr>
1145 <tr class="even">
1146 <td align="left">_______________________</td>
1147 <td align="left"></td>
1148 </tr>
1149 <tr class="odd">
1150 <td align="left">Column type frequency:</td>
1151 <td align="left"></td>
1152 </tr>
1153 <tr class="even">
1154 <td align="left">distance</td>
1155 <td align="left">10</td>
1156 </tr>
1157 <tr class="odd">
1158 <td align="left">________________________</td>
1159 <td align="left"></td>
1160 </tr>
1161 <tr class="even">
1162 <td align="left">Group variables</td>
1163 <td align="left">None</td>
1164 </tr>
1165 </tbody>
1166 </table>
1167 <p><strong>Variable type: distance</strong></p>
1168 <table>
1169 <thead>
1170 <tr class="header">
1171 <th align="left">skim_variable</th>
1172 <th align="right">n_missing</th>
1173 <th align="right">complete_rate</th>
1174 <th align="left">nearest</th>
1175 <th align="left">furthest</th>
1176 </tr>
1177 </thead>
1178 <tbody>
1179 <tr class="odd">
1180 <td align="left">Atlanta</td>
1181 <td align="right">0</td>
1182 <td align="right">1</td>
1183 <td align="left">Washington.DC</td>
1184 <td align="left">Seattle</td>
1185 </tr>
1186 <tr class="even">
1187 <td align="left">Chicago</td>
1188 <td align="right">0</td>
1189 <td align="right">1</td>
1190 <td align="left">Atlanta</td>
1191 <td align="left">SanFrancisco</td>
1192 </tr>
1193 <tr class="odd">
1194 <td align="left">Denver</td>
1195 <td align="right">0</td>
1196 <td align="right">1</td>
1197 <td align="left">LosAngeles</td>
1198 <td align="left">Miami</td>
1199 </tr>
1200 <tr class="even">
1201 <td align="left">Houston</td>
1202 <td align="right">0</td>
1203 <td align="right">1</td>
1204 <td align="left">Atlanta</td>
1205 <td align="left">Seattle</td>
1206 </tr>
1207 <tr class="odd">
1208 <td align="left">LosAngeles</td>
1209 <td align="right">0</td>
1210 <td align="right">1</td>
1211 <td align="left">SanFrancisco</td>
1212 <td align="left">NewYork</td>
1213 </tr>
1214 <tr class="even">
1215 <td align="left">Miami</td>
1216 <td align="right">0</td>
1217 <td align="right">1</td>
1218 <td align="left">Atlanta</td>
1219 <td align="left">Seattle</td>
1220 </tr>
1221 <tr class="odd">
1222 <td align="left">NewYork</td>
1223 <td align="right">0</td>
1224 <td align="right">1</td>
1225 <td align="left">Washington.DC</td>
1226 <td align="left">SanFrancisco</td>
1227 </tr>
1228 <tr class="even">
1229 <td align="left">SanFrancisco</td>
1230 <td align="right">0</td>
1231 <td align="right">1</td>
1232 <td align="left">LosAngeles</td>
1233 <td align="left">Miami</td>
1234 </tr>
1235 <tr class="odd">
1236 <td align="left">Seattle</td>
1237 <td align="right">0</td>
1238 <td align="right">1</td>
1239 <td align="left">SanFrancisco</td>
1240 <td align="left">Miami</td>
1241 </tr>
1242 <tr class="even">
1243 <td align="left">Washington.DC</td>
1244 <td align="right">0</td>
1245 <td align="right">1</td>
1246 <td align="left">NewYork</td>
1247 <td align="left">SanFrancisco</td>
1248 </tr>
1249 </tbody>
1250 </table>
1251 <p>The example above creates a new <em>function</em>, and you can call
1252 that function on a specific column with <code>distance</code> data to
1253 get the appropriate summary statistics. The <code>skim_with</code>
1254 factory also uses the default skimrs for things like factors,
1255 characters, and numerics. Therefore our <code>skim_with_dist</code> is
1256 like the regular <code>skim</code> function with the added ability to
1257 summarize <code>distance</code> columns.</p>
1258 <p>While this works for any data type and you can also include it within
1259 any package (assuming your users load skimr), there is a second, even
1260 better, approach. To take full advantage of <code>skimr</code>, we’ll
1261 dig a bit into its API.</p>
1262 </div>
1263 <div id="adding-new-methods" class="section level2">
1264 <h2>Adding new methods</h2>
1265 <p><code>skimr</code> has a lookup mechanism, based on the function
1266 <code>get_skimmers()</code>, to find default summary functions for each
1267 class. This is based on the S3 class system. You can learn more about it
1268 in <a href="https://adv-r.hadley.nz/s3.html"><em>Advanced
1269 R</em></a>.</p>
1270 <p>This requires that you add <code>skimr</code> to your list of
1271 dependencies.</p>
1272 <p>To export a new set of defaults for a data type, create a method for
1273 the generic function <code>get_skimmers</code>. Each of those methods
1274 returns an <code>sfl</code> (skimr function list) This is the same
1275 list-like data structure used in the <code>skim_with()</code> example
1276 above. But note! There is one key difference. When adding a generic we
1277 also want to identify the <code>skim_type</code> in the
1278 <code>sfl</code>. You will probably want to use
1279 <code>skimr::get_skimmers.distance()</code> but that will not work in a
1280 vignette.</p>
1281 <p>In a package you will want to export this.</p>
1282 <div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" tabindex="-1"></a><span class="co">#&#39; @importFrom skimr get_skimmers</span></span>
1283 <span id="cb24-2"><a href="#cb24-2" tabindex="-1"></a><span class="co">#&#39; @export</span></span>
1284 <span id="cb24-3"><a href="#cb24-3" tabindex="-1"></a>get_skimmers.distance <span class="ot">&lt;-</span> <span class="cf">function</span>(column) {</span>
1285 <span id="cb24-4"><a href="#cb24-4" tabindex="-1"></a> <span class="fu">sfl</span>(</span>
1286 <span id="cb24-5"><a href="#cb24-5" tabindex="-1"></a> <span class="at">skim_type =</span> <span class="st">&quot;distance&quot;</span>,</span>
1287 <span id="cb24-6"><a href="#cb24-6" tabindex="-1"></a> <span class="at">nearest =</span> get_nearest,</span>
1288 <span id="cb24-7"><a href="#cb24-7" tabindex="-1"></a> <span class="at">furthest =</span> get_furthest</span>
1289 <span id="cb24-8"><a href="#cb24-8" tabindex="-1"></a> )</span>
1290 <span id="cb24-9"><a href="#cb24-9" tabindex="-1"></a>}</span></code></pre></div>
1291 <p>The same strategy follows for other data types.</p>
1292 <ul>
1293 <li>Create a method</li>
1294 <li>return an <code>sfl</code></li>
1295 <li>make sure that the <code>skim_type</code> is included.</li>
1296 </ul>
1297 <p>Users of your package should load <code>skimr</code> to get the
1298 <code>skim()</code> function (although you could import and reexport
1299 it). Once loaded, a call to <code>get_default_skimmer_names()</code>
1300 will return defaults for your data types as well!</p>
1301 <div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" tabindex="-1"></a><span class="fu">get_default_skimmer_names</span>()</span></code></pre></div>
1302 <pre><code>## $AsIs
1303 ## [1] &quot;n_unique&quot; &quot;min_length&quot; &quot;max_length&quot;
1304 ##
1305 ## $Date
1306 ## [1] &quot;min&quot; &quot;max&quot; &quot;median&quot; &quot;n_unique&quot;
1307 ##
1308 ## $POSIXct
1309 ## [1] &quot;min&quot; &quot;max&quot; &quot;median&quot; &quot;n_unique&quot;
1310 ##
1311 ## $Timespan
1312 ## [1] &quot;min&quot; &quot;max&quot; &quot;median&quot; &quot;n_unique&quot;
1313 ##
1314 ## $character
1315 ## [1] &quot;min&quot; &quot;max&quot; &quot;empty&quot; &quot;n_unique&quot; &quot;whitespace&quot;
1316 ##
1317 ## $complex
1318 ## [1] &quot;mean&quot;
1319 ##
1320 ## $difftime
1321 ## [1] &quot;min&quot; &quot;max&quot; &quot;median&quot; &quot;n_unique&quot;
1322 ##
1323 ## $distance
1324 ## [1] &quot;nearest&quot; &quot;furthest&quot;
1325 ##
1326 ## $factor
1327 ## [1] &quot;ordered&quot; &quot;n_unique&quot; &quot;top_counts&quot;
1328 ##
1329 ## $haven_labelled
1330 ## [1] &quot;mean&quot; &quot;sd&quot; &quot;p0&quot; &quot;p25&quot; &quot;p50&quot; &quot;p75&quot; &quot;p100&quot; &quot;hist&quot;
1331 ##
1332 ## $list
1333 ## [1] &quot;n_unique&quot; &quot;min_length&quot; &quot;max_length&quot;
1334 ##
1335 ## $logical
1336 ## [1] &quot;mean&quot; &quot;count&quot;
1337 ##
1338 ## $numeric
1339 ## [1] &quot;mean&quot; &quot;sd&quot; &quot;p0&quot; &quot;p25&quot; &quot;p50&quot; &quot;p75&quot; &quot;p100&quot; &quot;hist&quot;
1340 ##
1341 ## $ts
1342 ## [1] &quot;start&quot; &quot;end&quot; &quot;frequency&quot; &quot;deltat&quot; &quot;mean&quot;
1343 ## [6] &quot;sd&quot; &quot;min&quot; &quot;max&quot; &quot;median&quot; &quot;line_graph&quot;</code></pre>
1344 <p>They will then be able to use <code>skim()</code> directly.</p>
1345 <div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb27-1"><a href="#cb27-1" tabindex="-1"></a><span class="fu">skim</span>(UScitiesD)</span></code></pre></div>
1346 <table>
1347 <caption>Data summary</caption>
1348 <tbody>
1349 <tr class="odd">
1350 <td align="left">Name</td>
1351 <td align="left">UScitiesD</td>
1352 </tr>
1353 <tr class="even">
1354 <td align="left">Number of rows</td>
1355 <td align="left">10</td>
1356 </tr>
1357 <tr class="odd">
1358 <td align="left">Number of columns</td>
1359 <td align="left">10</td>
1360 </tr>
1361 <tr class="even">
1362 <td align="left">_______________________</td>
1363 <td align="left"></td>
1364 </tr>
1365 <tr class="odd">
1366 <td align="left">Column type frequency:</td>
1367 <td align="left"></td>
1368 </tr>
1369 <tr class="even">
1370 <td align="left">distance</td>
1371 <td align="left">10</td>
1372 </tr>
1373 <tr class="odd">
1374 <td align="left">________________________</td>
1375 <td align="left"></td>
1376 </tr>
1377 <tr class="even">
1378 <td align="left">Group variables</td>
1379 <td align="left">None</td>
1380 </tr>
1381 </tbody>
1382 </table>
1383 <p><strong>Variable type: distance</strong></p>
1384 <table>
1385 <thead>
1386 <tr class="header">
1387 <th align="left">skim_variable</th>
1388 <th align="right">n_missing</th>
1389 <th align="right">complete_rate</th>
1390 <th align="left">nearest</th>
1391 <th align="left">furthest</th>
1392 </tr>
1393 </thead>
1394 <tbody>
1395 <tr class="odd">
1396 <td align="left">Atlanta</td>
1397 <td align="right">0</td>
1398 <td align="right">1</td>
1399 <td align="left">Washington.DC</td>
1400 <td align="left">Seattle</td>
1401 </tr>
1402 <tr class="even">
1403 <td align="left">Chicago</td>
1404 <td align="right">0</td>
1405 <td align="right">1</td>
1406 <td align="left">Atlanta</td>
1407 <td align="left">SanFrancisco</td>
1408 </tr>
1409 <tr class="odd">
1410 <td align="left">Denver</td>
1411 <td align="right">0</td>
1412 <td align="right">1</td>
1413 <td align="left">LosAngeles</td>
1414 <td align="left">Miami</td>
1415 </tr>
1416 <tr class="even">
1417 <td align="left">Houston</td>
1418 <td align="right">0</td>
1419 <td align="right">1</td>
1420 <td align="left">Atlanta</td>
1421 <td align="left">Seattle</td>
1422 </tr>
1423 <tr class="odd">
1424 <td align="left">LosAngeles</td>
1425 <td align="right">0</td>
1426 <td align="right">1</td>
1427 <td align="left">SanFrancisco</td>
1428 <td align="left">NewYork</td>
1429 </tr>
1430 <tr class="even">
1431 <td align="left">Miami</td>
1432 <td align="right">0</td>
1433 <td align="right">1</td>
1434 <td align="left">Atlanta</td>
1435 <td align="left">Seattle</td>
1436 </tr>
1437 <tr class="odd">
1438 <td align="left">NewYork</td>
1439 <td align="right">0</td>
1440 <td align="right">1</td>
1441 <td align="left">Washington.DC</td>
1442 <td align="left">SanFrancisco</td>
1443 </tr>
1444 <tr class="even">
1445 <td align="left">SanFrancisco</td>
1446 <td align="right">0</td>
1447 <td align="right">1</td>
1448 <td align="left">LosAngeles</td>
1449 <td align="left">Miami</td>
1450 </tr>
1451 <tr class="odd">
1452 <td align="left">Seattle</td>
1453 <td align="right">0</td>
1454 <td align="right">1</td>
1455 <td align="left">SanFrancisco</td>
1456 <td align="left">Miami</td>
1457 </tr>
1458 <tr class="even">
1459 <td align="left">Washington.DC</td>
1460 <td align="right">0</td>
1461 <td align="right">1</td>
1462 <td align="left">NewYork</td>
1463 <td align="left">SanFrancisco</td>
1464 </tr>
1465 </tbody>
1466 </table>
1467 </div>
1468 <div id="conclusion" class="section level2">
1469 <h2>Conclusion</h2>
1470 <p>This is a very simple example. For some packages the custom
1471 statistics will likely be much more complex. The flexibility of
1472 <code>skimr</code> allows you to manage that.</p>
1473 <p>Thanks to Jakub Nowosad, Tiernan Martin, Edzer Pebesma, Michael
1474 Sumner, and Kyle Butts for inspiring and helping with the development of
1475 this code.</p>
1476 </div>
1477 </div>
1478
1479
1480
1481 <!-- code folding -->
1482
1483
1484 <!-- dynamically load mathjax for compatibility with self-contained -->
1485 <script>
1486 (function () {
1487 var script = document.createElement("script");
1488 script.type = "text/javascript";
1489 script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
1490 document.getElementsByTagName("head")[0].appendChild(script);
1491 })();
1492 </script>
1493
1494 </body>
1495 </html>