/
fmtr.html
464 lines (436 loc) · 35.8 KB
/
fmtr.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<!DOCTYPE html>
<!-- Generated by pkgdown: do not edit by hand --><html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>fmtr • fmtr</title>
<!-- favicons --><link rel="icon" type="image/png" sizes="16x16" href="../favicon-16x16.png">
<link rel="icon" type="image/png" sizes="32x32" href="../favicon-32x32.png">
<link rel="apple-touch-icon" type="image/png" sizes="180x180" href="../apple-touch-icon.png">
<link rel="apple-touch-icon" type="image/png" sizes="120x120" href="../apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" type="image/png" sizes="76x76" href="../apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" type="image/png" sizes="60x60" href="../apple-touch-icon-60x60.png">
<!-- jquery --><script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js" integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo=" crossorigin="anonymous"></script><!-- Bootstrap --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha256-bZLfwXAP04zRMK2BjiO8iu9pf4FbLqX6zitd+tIvLhE=" crossorigin="anonymous">
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.4.1/js/bootstrap.min.js" integrity="sha256-nuL8/2cJ5NDSSwnKD8VqreErSWHtnEP9E7AySL+1ev4=" crossorigin="anonymous"></script><!-- bootstrap-toc --><link rel="stylesheet" href="../bootstrap-toc.css">
<script src="../bootstrap-toc.js"></script><!-- Font Awesome icons --><link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/all.min.css" integrity="sha256-mmgLkCYLUQbXn0B1SRqzHar6dCnv9oZFPEC1g1cwlkk=" crossorigin="anonymous">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.1/css/v4-shims.min.css" integrity="sha256-wZjR52fzng1pJHwx4aV2AO3yyTOXrcDW7jBpJtTwVxw=" crossorigin="anonymous">
<!-- clipboard.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.6/clipboard.min.js" integrity="sha256-inc5kl9MA1hkeYUt+EC3BhlIgyp/2jDIyBLS6k3UxPI=" crossorigin="anonymous"></script><!-- headroom.js --><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/headroom.min.js" integrity="sha256-AsUX4SJE1+yuDu5+mAVzJbuYNPHj/WroHuZ8Ir/CkE0=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/headroom/0.11.0/jQuery.headroom.min.js" integrity="sha256-ZX/yNShbjqsohH1k95liqY9Gd8uOiE1S4vZc+9KQ1K4=" crossorigin="anonymous"></script><!-- pkgdown --><link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script><script src="../extra.js"></script><meta property="og:title" content="fmtr">
<meta property="og:description" content="fmtr">
<meta property="og:image" content="https://fmtr.r-sassy.org/logo.png">
<!-- mathjax --><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js" integrity="sha256-nvJJv9wWKEm88qvoQl9ekL2J+k/RWIsaSScxxlsrv8k=" crossorigin="anonymous"></script><script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/config/TeX-AMS-MML_HTMLorMML.js" integrity="sha256-84DKXVJXs0/F8OTMzX4UR909+jtl4G7SPypPavF+GfA=" crossorigin="anonymous"></script><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body data-spy="scroll" data-target="#toc">
<div class="container template-article">
<header><div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<span class="navbar-brand">
<a class="navbar-link" href="../index.html">fmtr</a>
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="">1.6.2</span>
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav"></ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="../articles/fmtr.html">Get started</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
Articles
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="../articles/fmtr-fdata.html">Format Data Function</a>
</li>
<li>
<a href="../articles/fmtr-fapply.html">Format Apply Function</a>
</li>
<li>
<a href="../articles/fmtr-fcat.html">Format Catalogs</a>
</li>
<li>
<a href="../articles/fmtr-convenience.html">Convenience Functions</a>
</li>
<li>
<a href="../articles/fmtr-helpers.html">Helper Functions</a>
</li>
<li>
<a href="../articles/fmtr-example1.html">Complete Example</a>
</li>
<li>
<a href="../articles/fmtr-faq.html">Frequently Asked Questions</a>
</li>
</ul>
</li>
<li>
<a href="../reference/index.html">Reference</a>
</li>
<li>
<a href=".././articles/fmtr-faq.html">FAQ</a>
</li>
<li>
<a href="../news/index.html">Changelog</a>
</li>
<li>
<a href="https://github.com/dbosak01/fmtr/" class="external-link">
<span class="fab fa-github fa-lg"></span>
</a>
</li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
<!--/.container -->
</div>
<!--/.navbar -->
</header><div class="row">
<div class="col-md-9 contents">
<div class="page-header toc-ignore">
<h1 data-toc-skip>fmtr</h1>
<small class="dont-index">Source: <a href="https://github.com/dbosak01/fmtr/blob/HEAD/vignettes/fmtr.Rmd" class="external-link"><code>vignettes/fmtr.Rmd</code></a></small>
<div class="hidden name"><code>fmtr.Rmd</code></div>
</div>
<p>The <strong>fmtr</strong> package helps format data. The package aims
to simulate the basic functionality of SAS® formats, but with R. The
package contains several functions that make formatting simpler and more
powerful.</p>
<div class="section level3">
<h3 id="key-functions">Key Functions<a class="anchor" aria-label="anchor" href="#key-functions"></a>
</h3>
<p><strong>fmtr</strong> contains the following key functions:</p>
<ul>
<li>The <code><a href="../reference/fdata.html">fdata()</a></code> function to apply formatting to any data
frame or tibble.</li>
<li>The <code><a href="../reference/fapply.html">fapply()</a></code> function to apply formatting to any
vector.</li>
<li>The <code><a href="../reference/formats.html">formats()</a></code> and <code><a href="../reference/fattr.html">fattr()</a></code> functions to
easily assign formatting attributes.</li>
<li>The <code><a href="../reference/value.html">value()</a></code> and <code><a href="../reference/condition.html">condition()</a></code> functions to
create a user-defined format.</li>
<li>The <code><a href="../reference/fcat.html">fcat()</a></code> function to create a format catalog.</li>
<li>The <code><a href="../reference/flist.html">flist()</a></code> function to create a formatting list.</li>
</ul>
</div>
<div class="section level3">
<h3 id="how-to-use">How to Use<a class="anchor" aria-label="anchor" href="#how-to-use"></a>
</h3>
<p>The <strong>fmtr</strong> package builds heavily on existing R
formatting capabilities. For most R programmers, these functions are
well-known, and widely used. The examples below make use of standard R
formatting codes, such as those associated with the
<code><a href="https://rdrr.io/r/base/strptime.html" class="external-link">strptime()</a></code> and <code><a href="https://rdrr.io/r/base/sprintf.html" class="external-link">sprintf()</a></code> functions. The
standard R formatting codes are a flexible and compact way of defining a
format. If you are unfamiliar with R formatting codes, please see this
summary on the <code><a href="../reference/FormattingStrings.html">FormattingStrings()</a></code> page.</p>
<div class="section level4">
<h4 id="the-fapply-function">The <code>fapply()</code> Function<a class="anchor" aria-label="anchor" href="#the-fapply-function"></a>
</h4>
<p>The simplest way to introduce the <strong>fmtr</strong> package is to
examine the use of the <code><a href="../reference/fapply.html">fapply()</a></code> function.</p>
<div class="sourceCode" id="cb1"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create sample data vector</span></span>
<span><span class="va">v1</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1.483</span>, <span class="fl">5.29837</span>, <span class="fl">7.9472</span>, <span class="fl">8.684021</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Apply format</span></span>
<span><span class="fu"><a href="../reference/fapply.html">fapply</a></span><span class="op">(</span><span class="va">v1</span>, <span class="st">"%1.1f"</span><span class="op">)</span></span>
<span><span class="co"># [1] "1.5" "5.3" "7.9" "8.7"</span></span></code></pre></div>
<p>As you can see from the above example, the <code><a href="../reference/fapply.html">fapply()</a></code>
function typically takes two parameters: a vector and a format. In this
way, the <code><a href="../reference/fapply.html">fapply()</a></code> acts very much like a SAS®
<code>put</code> function.</p>
<p>Note that the format parameter can also be assigned as an attribute
on the vector. The <code><a href="../reference/fapply.html">fapply()</a></code> function will then pick up the
format attribute, and apply it to the input vector. The result is the
same:</p>
<div class="sourceCode" id="cb2"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create sample data vector</span></span>
<span><span class="va">v1</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1.483</span>, <span class="fl">5.29837</span>, <span class="fl">7.9472</span>, <span class="fl">8.684021</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Assign format attribute</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/attr.html" class="external-link">attr</a></span><span class="op">(</span><span class="va">v1</span>, <span class="st">"format"</span><span class="op">)</span> <span class="op"><-</span> <span class="st">"%1.1f"</span></span>
<span></span>
<span><span class="co"># Apply format</span></span>
<span><span class="fu"><a href="../reference/fapply.html">fapply</a></span><span class="op">(</span><span class="va">v1</span><span class="op">)</span></span>
<span><span class="co"># [1] "1.5" "5.3" "7.9" "8.7"</span></span></code></pre></div>
<p>Besides the format attribute, the <code><a href="../reference/fapply.html">fapply()</a></code> function will
also recognize attributes for <code>width</code> and
<code>justify</code>. These parameters allow you to control the width
and alignment of the data in the vector. If the width parameter is
larger than the width of the data, the value will be padded with spaces.
Here is an example:</p>
<div class="sourceCode" id="cb3"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create sample data vector</span></span>
<span><span class="va">v1</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1.483</span>, <span class="fl">5.29837</span>, <span class="fl">7.9472</span>, <span class="fl">8.684021</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Assign formatting attributes</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/attr.html" class="external-link">attr</a></span><span class="op">(</span><span class="va">v1</span>, <span class="st">"format"</span><span class="op">)</span> <span class="op"><-</span> <span class="st">"%1.1f"</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/attr.html" class="external-link">attr</a></span><span class="op">(</span><span class="va">v1</span>, <span class="st">"width"</span><span class="op">)</span> <span class="op"><-</span> <span class="fl">5</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/attr.html" class="external-link">attr</a></span><span class="op">(</span><span class="va">v1</span>, <span class="st">"justify"</span><span class="op">)</span> <span class="op"><-</span> <span class="st">"right"</span></span>
<span></span>
<span><span class="co"># Apply formatting attributes</span></span>
<span><span class="fu"><a href="../reference/fapply.html">fapply</a></span><span class="op">(</span><span class="va">v1</span><span class="op">)</span></span>
<span><span class="co"># [1] " 1.5" " 5.3" " 7.9" " 8.7"</span></span></code></pre></div>
<p>To help simplify assignment of these attributes, the
<strong>fmtr</strong> package includes the <code><a href="../reference/fattr.html">fattr()</a></code>
function, which allows you to set all the above attributes in one
function call. Here is an example using the <code><a href="../reference/fattr.html">fattr()</a></code>
function, that ends with the same result as the example above.</p>
<div class="sourceCode" id="cb4"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create sample data vector</span></span>
<span><span class="va">v1</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="fl">1.483</span>, <span class="fl">5.29837</span>, <span class="fl">7.9472</span>, <span class="fl">8.684021</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Assign formatting attributes</span></span>
<span><span class="va">v1</span> <span class="op"><-</span> <span class="fu"><a href="../reference/fattr.html">fattr</a></span><span class="op">(</span><span class="va">v1</span>, format <span class="op">=</span> <span class="st">"%1.1f"</span>, width <span class="op">=</span> <span class="fl">5</span>, justify <span class="op">=</span> <span class="st">"right"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Apply formatting attributes</span></span>
<span><span class="fu"><a href="../reference/fapply.html">fapply</a></span><span class="op">(</span><span class="va">v1</span><span class="op">)</span></span>
<span><span class="co"># [1] " 1.5" " 5.3" " 7.9" " 8.7"</span></span></code></pre></div>
<p>Note that <code><a href="../reference/fapply.html">fapply()</a></code> can accept several different types of
formats. The examples above focus on a simple numeric format. But
<code><a href="../reference/fapply.html">fapply()</a></code> also accepts date formats, a lookup list, a
user-defined format, a vectorized function, and a formatting list.</p>
<p>Here is an example showing the use of a lookup list:</p>
<div class="sourceCode" id="cb5"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create sample data vector</span></span>
<span><span class="va">v1</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"A"</span>, <span class="st">"C"</span>, <span class="st">"B"</span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create lookup vector</span></span>
<span><span class="va">v2</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span>A <span class="op">=</span> <span class="st">"Group A"</span>, B <span class="op">=</span> <span class="st">"Group B"</span>, C <span class="op">=</span> <span class="st">"Group C"</span><span class="op">)</span></span>
<span></span>
<span><span class="fu"><a href="../reference/fapply.html">fapply</a></span><span class="op">(</span><span class="va">v1</span>, <span class="va">v2</span><span class="op">)</span></span>
<span><span class="co"># [1] "Group A" "Group B" "Group A" "Group C" "Group B"</span></span></code></pre></div>
</div>
<div class="section level4">
<h4 id="a-user-defined-format">A User-Defined Format<a class="anchor" aria-label="anchor" href="#a-user-defined-format"></a>
</h4>
<p>The weakness with using a named vector as a lookup list, as in the
above example, is that there is no way to include any sort of logic in
the lookup. For instance, if your data has NA values, you may want to
handle those differently from the valid input values. Or you may want to
define a default value if the input data does not match any of the
lookup keys.</p>
<p>For these reasons, the <strong>fmtr</strong> package provides a
<em>user-defined format</em>. This concept was taken directly from SAS®
software. The functions that create a user-defined format are
<code><a href="../reference/value.html">value()</a></code> and <code><a href="../reference/condition.html">condition()</a></code>.</p>
<p>A condition accepts an expression and a label. The expression
determines which label is assigned. For the expression, you can use
logical operators like “&” and “|”, and relational operators like
“>” and “<”. The data value is identified with a variable “x”.
Here is an example:</p>
<div class="sourceCode" id="cb6"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Create sample data vector</span></span>
<span><span class="va">v1</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/c.html" class="external-link">c</a></span><span class="op">(</span><span class="st">"A"</span>, <span class="st">"B"</span>, <span class="st">"E"</span>, <span class="st">"A"</span>, <span class="cn">NA</span>, <span class="st">"C"</span>, <span class="st">"D"</span><span class="op">)</span></span>
<span></span>
<span><span class="va">u1</span> <span class="op"><-</span> <span class="fu"><a href="../reference/value.html">value</a></span><span class="op">(</span><span class="fu"><a href="../reference/condition.html">condition</a></span><span class="op">(</span><span class="va">x</span> <span class="op">==</span> <span class="st">"A"</span>, <span class="st">"Group A"</span><span class="op">)</span>,</span>
<span> <span class="fu"><a href="../reference/condition.html">condition</a></span><span class="op">(</span><span class="va">x</span> <span class="op">==</span> <span class="st">"B"</span>, <span class="st">"Group B"</span><span class="op">)</span>,</span>
<span> <span class="fu"><a href="../reference/condition.html">condition</a></span><span class="op">(</span><span class="va">x</span> <span class="op">==</span> <span class="st">"C"</span> <span class="op">|</span> <span class="va">x</span> <span class="op">==</span> <span class="st">"D"</span>, <span class="st">"Group C/D"</span><span class="op">)</span>, </span>
<span> <span class="fu"><a href="../reference/condition.html">condition</a></span><span class="op">(</span><span class="cn">TRUE</span>, <span class="st">"Other"</span><span class="op">)</span><span class="op">)</span></span>
<span> </span>
<span><span class="fu"><a href="../reference/fapply.html">fapply</a></span><span class="op">(</span><span class="va">v1</span>, <span class="va">u1</span><span class="op">)</span> </span>
<span><span class="co"># [1] "Group A" "Group B" "Other" "Group A" "Other" "Group C/D" "Group C/D"</span></span></code></pre></div>
<p>Notice that the user-defined format gives you much more capabilities
than a simple lookup vector. It allows you to perform categorization,
and assign a default. Additionally, the NA missing value does not crash
the function. The NA simply falls into the default category. If there is
no default category, any values which do not correspond to a category
will fall through the format unaltered.</p>
</div>
<div class="section level4">
<h4 id="the-fdata-function">The <code>fdata()</code> Function<a class="anchor" aria-label="anchor" href="#the-fdata-function"></a>
</h4>
<p>The <code><a href="../reference/fdata.html">fdata()</a></code> function works very much the same way as
<code><a href="../reference/fapply.html">fapply()</a></code>, but with data frames and tibbles instead of
vectors. In fact, under the hood, <code><a href="../reference/fdata.html">fdata()</a></code> is simply calling
<code><a href="../reference/fapply.html">fapply()</a></code> for each column in the data frame.</p>
<p>Like the <code><a href="../reference/fapply.html">fapply()</a></code> function, formatting may be assigned
to data frame columns using the <strong>format</strong>,
<strong>width</strong>, and <strong>justify</strong> attributes.
Formatting is then applied by calling the <code><a href="../reference/fdata.html">fdata()</a></code> function,
and passing the data frame as the first parameter. <code><a href="../reference/fdata.html">fdata()</a></code>
will then return a new data frame with the specified formatting applied.
This method of formatting provides much greater control than the base R
<code><a href="https://rdrr.io/r/base/format.html" class="external-link">format()</a></code> function.</p>
<div class="sourceCode" id="cb7"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Construct data frame from state vectors</span></span>
<span><span class="va">df</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>state <span class="op">=</span> <span class="va">state.abb</span>, area <span class="op">=</span> <span class="va">state.area</span><span class="op">)</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">10</span>, <span class="op">]</span></span>
<span></span>
<span><span class="co"># Calculate percentages</span></span>
<span><span class="va">df</span><span class="op">$</span><span class="va">pct</span> <span class="op"><-</span> <span class="va">df</span><span class="op">$</span><span class="va">area</span> <span class="op">/</span> <span class="fu"><a href="https://rdrr.io/r/base/sum.html" class="external-link">sum</a></span><span class="op">(</span><span class="va">state.area</span><span class="op">)</span> <span class="op">*</span> <span class="fl">100</span></span>
<span></span>
<span><span class="co"># Before formatting </span></span>
<span><span class="va">df</span></span>
<span><span class="co"># state area pct</span></span>
<span><span class="co"># 1 AL 51609 1.42629378</span></span>
<span><span class="co"># 2 AK 589757 16.29883824</span></span>
<span><span class="co"># 3 AZ 113909 3.14804973</span></span>
<span><span class="co"># 4 AR 53104 1.46761040</span></span>
<span><span class="co"># 5 CA 158693 4.38572418</span></span>
<span><span class="co"># 6 CO 104247 2.88102556</span></span>
<span><span class="co"># 7 CT 5009 0.13843139</span></span>
<span><span class="co"># 8 DE 2057 0.05684835</span></span>
<span><span class="co"># 9 FL 58560 1.61839532</span></span>
<span><span class="co"># 10 GA 58876 1.62712846</span></span>
<span></span>
<span><span class="co"># Create state name lookup list</span></span>
<span><span class="va">name_lookup</span> <span class="op"><-</span> <span class="va">state.name</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/names.html" class="external-link">names</a></span><span class="op">(</span><span class="va">name_lookup</span><span class="op">)</span> <span class="op"><-</span> <span class="va">state.abb</span></span>
<span></span>
<span><span class="co"># Assign formats</span></span>
<span><span class="fu"><a href="../reference/formats.html">formats</a></span><span class="op">(</span><span class="va">df</span><span class="op">)</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/list.html" class="external-link">list</a></span><span class="op">(</span>state <span class="op">=</span> <span class="va">name_lookup</span>, </span>
<span> area <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">x</span>, big.mark <span class="op">=</span> <span class="st">","</span><span class="op">)</span>, </span>
<span> pct <span class="op">=</span> <span class="st">"%.1f%%"</span><span class="op">)</span> </span>
<span></span>
<span><span class="co"># Apply formats</span></span>
<span><span class="fu"><a href="../reference/fdata.html">fdata</a></span><span class="op">(</span><span class="va">df</span><span class="op">)</span></span>
<span><span class="co"># state area pct</span></span>
<span><span class="co"># 1 Alabama 51,609 1.4%</span></span>
<span><span class="co"># 2 Alaska 589,757 16.3%</span></span>
<span><span class="co"># 3 Arizona 113,909 3.1%</span></span>
<span><span class="co"># 4 Arkansas 53,104 1.5%</span></span>
<span><span class="co"># 5 California 158,693 4.4%</span></span>
<span><span class="co"># 6 Colorado 104,247 2.9%</span></span>
<span><span class="co"># 7 Connecticut 5,009 0.1%</span></span>
<span><span class="co"># 8 Delaware 2,057 0.1%</span></span>
<span><span class="co"># 9 Florida 58,560 1.6%</span></span>
<span><span class="co"># 10 Georgia 58,876 1.6%</span></span></code></pre></div>
<p>In the above example, observe that the <code><a href="../reference/formats.html">formats()</a></code>
function assigns the format attribute for multiple columns. This
assignment is accomplished by sending a named list into the
<code><a href="../reference/formats.html">formats()</a></code> function, where the names in the list correspond
to the column names of the data frame. Also note the use of a lookup
style format for the state names, and an anonymous vectorized format
function for the state area.</p>
</div>
<div class="section level4">
<h4 id="the-fcat-function">The <code>fcat()</code> Function<a class="anchor" aria-label="anchor" href="#the-fcat-function"></a>
</h4>
<p>One of the benefits of the above method of formatting is that the
data frame attributes can be stored with the data frame, and reapplied
in the future. But what if you want to apply the same set of formats to
a different data frame?</p>
<p>That is where you need a <em>format catalog</em>.</p>
<p>The format catalog is a collection of formats that can be saved and
reused. A format catalog is created with an <code><a href="../reference/fcat.html">fcat()</a></code>
function. To create a format catalog, you call the <code><a href="../reference/fcat.html">fcat()</a></code>
function, passing a set of name/format pairs. In this case, the name of
the format is a generic format name. It does not have to correspond to a
column name. You may name the formats anything you want. The formats can
be accessed in the catalog using dollar sign (“$”) list notation.</p>
<div class="sourceCode" id="cb8"><pre class="downlit sourceCode r">
<code class="sourceCode R"><span><span class="kw"><a href="https://rdrr.io/r/base/library.html" class="external-link">library</a></span><span class="op">(</span><span class="va"><a href="https://fmtr.r-sassy.org">fmtr</a></span><span class="op">)</span></span>
<span></span>
<span><span class="co"># Construct data frame from state vectors</span></span>
<span><span class="va">df</span> <span class="op"><-</span> <span class="fu"><a href="https://rdrr.io/r/base/data.frame.html" class="external-link">data.frame</a></span><span class="op">(</span>state <span class="op">=</span> <span class="va">state.abb</span>, area <span class="op">=</span> <span class="va">state.area</span><span class="op">)</span><span class="op">[</span><span class="fl">1</span><span class="op">:</span><span class="fl">10</span>, <span class="op">]</span></span>
<span></span>
<span><span class="co"># Calculate percentages</span></span>
<span><span class="va">df</span><span class="op">$</span><span class="va">pct</span> <span class="op"><-</span> <span class="va">df</span><span class="op">$</span><span class="va">area</span> <span class="op">/</span> <span class="fu"><a href="https://rdrr.io/r/base/sum.html" class="external-link">sum</a></span><span class="op">(</span><span class="va">state.area</span><span class="op">)</span> <span class="op">*</span> <span class="fl">100</span></span>
<span></span>
<span><span class="co"># Before formatting </span></span>
<span><span class="va">df</span></span>
<span><span class="co"># state area pct</span></span>
<span><span class="co"># 1 AL 51609 1.42629378</span></span>
<span><span class="co"># 2 AK 589757 16.29883824</span></span>
<span><span class="co"># 3 AZ 113909 3.14804973</span></span>
<span><span class="co"># 4 AR 53104 1.46761040</span></span>
<span><span class="co"># 5 CA 158693 4.38572418</span></span>
<span><span class="co"># 6 CO 104247 2.88102556</span></span>
<span><span class="co"># 7 CT 5009 0.13843139</span></span>
<span><span class="co"># 8 DE 2057 0.05684835</span></span>
<span><span class="co"># 9 FL 58560 1.61839532</span></span>
<span><span class="co"># 10 GA 58876 1.62712846</span></span>
<span></span>
<span><span class="co"># Create state name lookup list</span></span>
<span><span class="va">name_lookup</span> <span class="op"><-</span> <span class="va">state.name</span></span>
<span><span class="fu"><a href="https://rdrr.io/r/base/names.html" class="external-link">names</a></span><span class="op">(</span><span class="va">name_lookup</span><span class="op">)</span> <span class="op"><-</span> <span class="va">state.abb</span></span>
<span></span>
<span><span class="co"># Assign formats to format catalog</span></span>
<span><span class="va">cat1</span> <span class="op"><-</span> <span class="fu"><a href="../reference/fcat.html">fcat</a></span><span class="op">(</span>state <span class="op">=</span> <span class="va">name_lookup</span>, </span>
<span> area <span class="op">=</span> <span class="kw">function</span><span class="op">(</span><span class="va">x</span><span class="op">)</span> <span class="fu"><a href="https://rdrr.io/r/base/format.html" class="external-link">format</a></span><span class="op">(</span><span class="va">x</span>, big.mark <span class="op">=</span> <span class="st">","</span><span class="op">)</span>, </span>
<span> pct <span class="op">=</span> <span class="st">"%.1f%%"</span><span class="op">)</span> </span>
<span> </span>
<span><span class="co"># Apply a format from the catalog using fapply</span></span>
<span><span class="fu"><a href="../reference/fapply.html">fapply</a></span><span class="op">(</span><span class="va">df</span><span class="op">$</span><span class="va">pct</span>, <span class="va">cat1</span><span class="op">$</span><span class="va">pct</span><span class="op">)</span></span>
<span><span class="co"># [1] "1.4%" "16.3%" "3.1%" "1.5%" "4.4%" "2.9%" "0.1%" "0.1%" "1.6%" "1.6%"</span></span>
<span></span>
<span><span class="co"># Assign formats from the catalog to format attributes</span></span>
<span><span class="fu"><a href="../reference/formats.html">formats</a></span><span class="op">(</span><span class="va">df</span><span class="op">)</span> <span class="op"><-</span> <span class="va">cat1</span></span>
<span></span>
<span><span class="co"># Apply formats</span></span>
<span><span class="fu"><a href="../reference/fdata.html">fdata</a></span><span class="op">(</span><span class="va">df</span><span class="op">)</span></span>
<span><span class="co"># state area pct</span></span>
<span><span class="co"># 1 Alabama 51,609 1.4%</span></span>
<span><span class="co"># 2 Alaska 589,757 16.3%</span></span>
<span><span class="co"># 3 Arizona 113,909 3.1%</span></span>
<span><span class="co"># 4 Arkansas 53,104 1.5%</span></span>
<span><span class="co"># 5 California 158,693 4.4%</span></span>
<span><span class="co"># 6 Colorado 104,247 2.9%</span></span>
<span><span class="co"># 7 Connecticut 5,009 0.1%</span></span>
<span><span class="co"># 8 Delaware 2,057 0.1%</span></span>
<span><span class="co"># 9 Florida 58,560 1.6%</span></span>
<span><span class="co"># 10 Georgia 58,876 1.6%</span></span></code></pre></div>
<p>In normal use, of course, the format catalog would likely be created
in a separate script and saved to a file using the
<code><a href="../reference/write.fcat.html">write.fcat()</a></code> function. The format catalog can then be read
by any number of programs using the <code><a href="../reference/read.fcat.html">read.fcat()</a></code> function,
and the formats in the catalog can be applied as needed to your
data.</p>
</div>
</div>
<div class="section level3">
<h3 id="next-steps">Next Steps<a class="anchor" aria-label="anchor" href="#next-steps"></a>
</h3>
<p>For additional reinforcement of the topics presented above, please
read the following articles:</p>
<ul>
<li><a href="fmtr-fdata.html">Format Data Function</a></li>
<li><a href="fmtr-fapply.html">Format Apply Function</a></li>
<li><a href="fmtr-fcat.html">Format Catalogs</a></li>
<li><a href="fmtr-convenience.html">Convenience Functions</a></li>
<li><a href="fmtr-helpers.html">Helper Functions</a></li>
</ul>
</div>
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="pkgdown-sidebar">
</div>
</div>
<footer><div class="copyright">
<p></p>
<p>Developed by David Bosak.</p>
</div>
<div class="pkgdown">
<p></p>
<p>Site built with <a href="https://pkgdown.r-lib.org/" class="external-link">pkgdown</a> 2.0.7.</p>
</div>
</footer>
</div>
</body>
</html>