| 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 |
<meta name="author" content="Max Gordon" /> |
| 14 |
|
| 15 |
<meta name="date" content="2026-04-21" /> |
| 16 |
|
| 17 |
<title>Text formatters</title> |
| 18 |
|
| 19 |
<script>// Pandoc 2.9 adds attributes on both header and div. We remove the former (to |
| 20 |
// be compatible with the behavior of Pandoc < 2.8). |
| 21 |
document.addEventListener('DOMContentLoaded', function(e) { |
| 22 |
var hs = document.querySelectorAll("div.section[class*='level'] > :first-child"); |
| 23 |
var i, h, a; |
| 24 |
for (i = 0; i < hs.length; i++) { |
| 25 |
h = hs[i]; |
| 26 |
if (!/^h[1-6]$/i.test(h.tagName)) continue; // it should be a header h1-h6 |
| 27 |
a = h.attributes; |
| 28 |
while (a.length > 0) h.removeAttribute(a[0].name); |
| 29 |
} |
| 30 |
}); |
| 31 |
</script> |
| 32 |
|
| 33 |
<style type="text/css"> |
| 34 |
code{white-space: pre-wrap;} |
| 35 |
span.smallcaps{font-variant: small-caps;} |
| 36 |
span.underline{text-decoration: underline;} |
| 37 |
div.column{display: inline-block; vertical-align: top; width: 50%;} |
| 38 |
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;} |
| 39 |
ul.task-list{list-style: none;} |
| 40 |
</style> |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
<style type="text/css"> |
| 45 |
code { |
| 46 |
white-space: pre; |
| 47 |
} |
| 48 |
.sourceCode { |
| 49 |
overflow: visible; |
| 50 |
} |
| 51 |
</style> |
| 52 |
<style type="text/css" data-origin="pandoc"> |
| 53 |
html { -webkit-text-size-adjust: 100%; } |
| 54 |
pre > code.sourceCode { white-space: pre; position: relative; } |
| 55 |
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; } |
| 56 |
pre > code.sourceCode > span:empty { height: 1.2em; } |
| 57 |
.sourceCode { overflow: visible; } |
| 58 |
code.sourceCode > span { color: inherit; text-decoration: inherit; } |
| 59 |
div.sourceCode { margin: 1em 0; } |
| 60 |
pre.sourceCode { margin: 0; } |
| 61 |
@media screen { |
| 62 |
div.sourceCode { overflow: auto; } |
| 63 |
} |
| 64 |
@media print { |
| 65 |
pre > code.sourceCode { white-space: pre-wrap; } |
| 66 |
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; } |
| 67 |
} |
| 68 |
pre.numberSource code |
| 69 |
{ counter-reset: source-line 0; } |
| 70 |
pre.numberSource code > span |
| 71 |
{ position: relative; left: -4em; counter-increment: source-line; } |
| 72 |
pre.numberSource code > span > a:first-child::before |
| 73 |
{ content: counter(source-line); |
| 74 |
position: relative; left: -1em; text-align: right; vertical-align: baseline; |
| 75 |
border: none; display: inline-block; |
| 76 |
-webkit-touch-callout: none; -webkit-user-select: none; |
| 77 |
-khtml-user-select: none; -moz-user-select: none; |
| 78 |
-ms-user-select: none; user-select: none; |
| 79 |
padding: 0 4px; width: 4em; |
| 80 |
color: #aaaaaa; |
| 81 |
} |
| 82 |
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa; padding-left: 4px; } |
| 83 |
div.sourceCode |
| 84 |
{ } |
| 85 |
@media screen { |
| 86 |
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; } |
| 87 |
} |
| 88 |
code span.al { color: #ff0000; font-weight: bold; } |
| 89 |
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 90 |
code span.at { color: #7d9029; } |
| 91 |
code span.bn { color: #40a070; } |
| 92 |
code span.bu { color: #008000; } |
| 93 |
code span.cf { color: #007020; font-weight: bold; } |
| 94 |
code span.ch { color: #4070a0; } |
| 95 |
code span.cn { color: #880000; } |
| 96 |
code span.co { color: #60a0b0; font-style: italic; } |
| 97 |
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 98 |
code span.do { color: #ba2121; font-style: italic; } |
| 99 |
code span.dt { color: #902000; } |
| 100 |
code span.dv { color: #40a070; } |
| 101 |
code span.er { color: #ff0000; font-weight: bold; } |
| 102 |
code span.ex { } |
| 103 |
code span.fl { color: #40a070; } |
| 104 |
code span.fu { color: #06287e; } |
| 105 |
code span.im { color: #008000; font-weight: bold; } |
| 106 |
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 107 |
code span.kw { color: #007020; font-weight: bold; } |
| 108 |
code span.op { color: #666666; } |
| 109 |
code span.ot { color: #007020; } |
| 110 |
code span.pp { color: #bc7a00; } |
| 111 |
code span.sc { color: #4070a0; } |
| 112 |
code span.ss { color: #bb6688; } |
| 113 |
code span.st { color: #4070a0; } |
| 114 |
code span.va { color: #19177c; } |
| 115 |
code span.vs { color: #4070a0; } |
| 116 |
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } |
| 117 |
</style> |
| 118 |
<script> |
| 119 |
// apply pandoc div.sourceCode style to pre.sourceCode instead |
| 120 |
(function() { |
| 121 |
var sheets = document.styleSheets; |
| 122 |
for (var i = 0; i < sheets.length; i++) { |
| 123 |
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue; |
| 124 |
try { var rules = sheets[i].cssRules; } catch (e) { continue; } |
| 125 |
var j = 0; |
| 126 |
while (j < rules.length) { |
| 127 |
var rule = rules[j]; |
| 128 |
// check if there is a div.sourceCode rule |
| 129 |
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") { |
| 130 |
j++; |
| 131 |
continue; |
| 132 |
} |
| 133 |
var style = rule.style.cssText; |
| 134 |
// check if color or background-color is set |
| 135 |
if (rule.style.color === '' && rule.style.backgroundColor === '') { |
| 136 |
j++; |
| 137 |
continue; |
| 138 |
} |
| 139 |
// replace div.sourceCode by a pre.sourceCode rule |
| 140 |
sheets[i].deleteRule(j); |
| 141 |
sheets[i].insertRule('pre.sourceCode{' + style + '}', j); |
| 142 |
} |
| 143 |
} |
| 144 |
})(); |
| 145 |
</script> |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
<style type="text/css">body { |
| 151 |
background-color: #fff; |
| 152 |
margin: 1em auto; |
| 153 |
max-width: 700px; |
| 154 |
overflow: visible; |
| 155 |
padding-left: 2em; |
| 156 |
padding-right: 2em; |
| 157 |
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif; |
| 158 |
font-size: 14px; |
| 159 |
line-height: 1.35; |
| 160 |
} |
| 161 |
#header { |
| 162 |
text-align: center; |
| 163 |
} |
| 164 |
#TOC { |
| 165 |
clear: both; |
| 166 |
margin: 0 0 10px 10px; |
| 167 |
padding: 4px; |
| 168 |
width: 400px; |
| 169 |
border: 1px solid #CCCCCC; |
| 170 |
border-radius: 5px; |
| 171 |
background-color: #f6f6f6; |
| 172 |
font-size: 13px; |
| 173 |
line-height: 1.3; |
| 174 |
} |
| 175 |
#TOC .toctitle { |
| 176 |
font-weight: bold; |
| 177 |
font-size: 15px; |
| 178 |
margin-left: 5px; |
| 179 |
} |
| 180 |
#TOC ul { |
| 181 |
padding-left: 40px; |
| 182 |
margin-left: -1.5em; |
| 183 |
margin-top: 5px; |
| 184 |
margin-bottom: 5px; |
| 185 |
} |
| 186 |
#TOC ul ul { |
| 187 |
margin-left: -2em; |
| 188 |
} |
| 189 |
#TOC li { |
| 190 |
line-height: 16px; |
| 191 |
} |
| 192 |
table { |
| 193 |
margin: 1em auto; |
| 194 |
} |
| 195 |
p { |
| 196 |
margin: 0.5em 0; |
| 197 |
} |
| 198 |
blockquote { |
| 199 |
background-color: #f6f6f6; |
| 200 |
padding: 0.25em 0.75em; |
| 201 |
} |
| 202 |
hr { |
| 203 |
border-style: solid; |
| 204 |
border: none; |
| 205 |
border-top: 1px solid #777; |
| 206 |
margin: 28px 0; |
| 207 |
} |
| 208 |
dl { |
| 209 |
margin-left: 0; |
| 210 |
} |
| 211 |
dl dd { |
| 212 |
margin-bottom: 13px; |
| 213 |
margin-left: 13px; |
| 214 |
} |
| 215 |
dl dt { |
| 216 |
font-weight: bold; |
| 217 |
} |
| 218 |
ul { |
| 219 |
margin-top: 0; |
| 220 |
} |
| 221 |
ul li { |
| 222 |
list-style: circle outside; |
| 223 |
} |
| 224 |
ul ul { |
| 225 |
margin-bottom: 0; |
| 226 |
} |
| 227 |
pre, code { |
| 228 |
background-color: #f7f7f7; |
| 229 |
border-radius: 3px; |
| 230 |
color: #333; |
| 231 |
} |
| 232 |
pre { |
| 233 |
white-space: pre-wrap; |
| 234 |
border-radius: 3px; |
| 235 |
margin: 5px 0px 10px 0px; |
| 236 |
padding: 10px; |
| 237 |
} |
| 238 |
pre:not([class]) { |
| 239 |
background-color: #f7f7f7; |
| 240 |
} |
| 241 |
code { |
| 242 |
font-family: Consolas, Monaco, 'Courier New', monospace; |
| 243 |
font-size: 85%; |
| 244 |
} |
| 245 |
p > code, li > code { |
| 246 |
padding: 2px 0px; |
| 247 |
} |
| 248 |
div.figure { |
| 249 |
text-align: center; |
| 250 |
} |
| 251 |
img { |
| 252 |
background-color: #FFFFFF; |
| 253 |
padding: 2px; |
| 254 |
border: 1px solid #DDDDDD; |
| 255 |
border-radius: 3px; |
| 256 |
border: 1px solid #CCCCCC; |
| 257 |
margin: 0 5px; |
| 258 |
} |
| 259 |
h1 { |
| 260 |
margin-top: 0; |
| 261 |
font-size: 35px; |
| 262 |
line-height: 40px; |
| 263 |
} |
| 264 |
h2 { |
| 265 |
border-bottom: 4px solid #f7f7f7; |
| 266 |
padding-top: 10px; |
| 267 |
padding-bottom: 2px; |
| 268 |
font-size: 145%; |
| 269 |
} |
| 270 |
h3 { |
| 271 |
border-bottom: 2px solid #f7f7f7; |
| 272 |
padding-top: 10px; |
| 273 |
font-size: 120%; |
| 274 |
} |
| 275 |
h4 { |
| 276 |
border-bottom: 1px solid #f7f7f7; |
| 277 |
margin-left: 8px; |
| 278 |
font-size: 105%; |
| 279 |
} |
| 280 |
h5, h6 { |
| 281 |
border-bottom: 1px solid #ccc; |
| 282 |
font-size: 105%; |
| 283 |
} |
| 284 |
a { |
| 285 |
color: #0033dd; |
| 286 |
text-decoration: none; |
| 287 |
} |
| 288 |
a:hover { |
| 289 |
color: #6666ff; } |
| 290 |
a:visited { |
| 291 |
color: #800080; } |
| 292 |
a:visited:hover { |
| 293 |
color: #BB00BB; } |
| 294 |
a[href^="http:"] { |
| 295 |
text-decoration: underline; } |
| 296 |
a[href^="https:"] { |
| 297 |
text-decoration: underline; } |
| 298 |
|
| 299 |
code > span.kw { color: #555; font-weight: bold; } |
| 300 |
code > span.dt { color: #902000; } |
| 301 |
code > span.dv { color: #40a070; } |
| 302 |
code > span.bn { color: #d14; } |
| 303 |
code > span.fl { color: #d14; } |
| 304 |
code > span.ch { color: #d14; } |
| 305 |
code > span.st { color: #d14; } |
| 306 |
code > span.co { color: #888888; font-style: italic; } |
| 307 |
code > span.ot { color: #007020; } |
| 308 |
code > span.al { color: #ff0000; font-weight: bold; } |
| 309 |
code > span.fu { color: #900; font-weight: bold; } |
| 310 |
code > span.er { color: #a61717; background-color: #e3d2d2; } </style> |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
</head> |
| 316 |
|
| 317 |
<body> |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
<h1 class="title toc-ignore">Text formatters</h1> |
| 323 |
<h4 class="author">Max Gordon</h4> |
| 324 |
<h4 class="date">2026-04-21</h4> |
| 325 |
|
| 326 |
|
| 327 |
<div id="TOC"> |
| 328 |
<ul> |
| 329 |
<li><a href="#text-formatters" id="toc-text-formatters">Text |
| 330 |
formatters</a> |
| 331 |
<ul> |
| 332 |
<li><a href="#txtround" id="toc-txtround">txtRound</a> |
| 333 |
<ul> |
| 334 |
<li><a href="#singlevector-values" id="toc-singlevector-values">Single/vector values</a></li> |
| 335 |
<li><a href="#data-frames" id="toc-data-frames">Data frames</a></li> |
| 336 |
<li><a href="#matrix" id="toc-matrix">Matrix</a></li> |
| 337 |
</ul></li> |
| 338 |
<li><a href="#txtint" id="toc-txtint">txtInt</a></li> |
| 339 |
<li><a href="#txtpval" id="toc-txtpval">txtPval</a></li> |
| 340 |
<li><a href="#txtmergelines" id="toc-txtmergelines">txtMergeLines</a></li> |
| 341 |
</ul></li> |
| 342 |
</ul> |
| 343 |
</div> |
| 344 |
|
| 345 |
<div id="text-formatters" class="section level1"> |
| 346 |
<h1>Text formatters</h1> |
| 347 |
<p>Bundled with this package are some text formatting functions. The |
| 348 |
purpose of these is to convert numeric values into character/text that |
| 349 |
is more pleasent in publication tables.</p> |
| 350 |
<div id="txtround" class="section level2"> |
| 351 |
<h2>txtRound</h2> |
| 352 |
<p>While <code>base::round()</code> is an excellent function in most |
| 353 |
cases we often want a table to retain trailing 0:s. E.g.</p> |
| 354 |
<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>(htmlTable)</span> |
| 355 |
<span id="cb1-2"><a href="#cb1-2" tabindex="-1"></a><span class="fu">library</span>(dplyr)</span> |
| 356 |
<span id="cb1-3"><a href="#cb1-3" tabindex="-1"></a><span class="fu">library</span>(magrittr)</span> |
| 357 |
<span id="cb1-4"><a href="#cb1-4" tabindex="-1"></a><span class="fu">data</span>(<span class="st">"mtcars"</span>)</span> |
| 358 |
<span id="cb1-5"><a href="#cb1-5" tabindex="-1"></a></span> |
| 359 |
<span id="cb1-6"><a href="#cb1-6" tabindex="-1"></a>mtcars <span class="sc">%<>%</span></span> |
| 360 |
<span id="cb1-7"><a href="#cb1-7" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">am =</span> <span class="fu">factor</span>(am, <span class="at">levels =</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"Automatic"</span>, <span class="st">"Manual"</span>)),</span> |
| 361 |
<span id="cb1-8"><a href="#cb1-8" tabindex="-1"></a> <span class="at">vs =</span> <span class="fu">factor</span>(vs, <span class="at">levels =</span> <span class="dv">0</span><span class="sc">:</span><span class="dv">1</span>, <span class="at">labels =</span> <span class="fu">c</span>(<span class="st">"V-shaped"</span>, <span class="st">"straight"</span>)))</span> |
| 362 |
<span id="cb1-9"><a href="#cb1-9" tabindex="-1"></a></span> |
| 363 |
<span id="cb1-10"><a href="#cb1-10" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span> |
| 364 |
<span id="cb1-11"><a href="#cb1-11" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">3</span>) <span class="sc">%>%</span> </span> |
| 365 |
<span id="cb1-12"><a href="#cb1-12" tabindex="-1"></a> <span class="fu">select</span>(<span class="at">Transmission =</span> am, <span class="at">Gas =</span> mpg, <span class="at">Weight =</span> wt) <span class="sc">%>%</span> </span> |
| 366 |
<span id="cb1-13"><a href="#cb1-13" tabindex="-1"></a> <span class="fu">htmlTable</span>()</span></code></pre></div> |
| 367 |
<table class="gmisc_table" style="border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;"> |
| 368 |
<thead> |
| 369 |
<tr> |
| 370 |
<th style="border-bottom: 1px solid grey; border-top: 2px solid grey;"> |
| 371 |
</th> |
| 372 |
<th style="margin: 0; padding: 0; font-weight: 900; vertical-align: middle; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;"> |
| 373 |
Transmission |
| 374 |
</th> |
| 375 |
<th style="margin: 0; padding: 0; font-weight: 900; vertical-align: middle; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;"> |
| 376 |
Gas |
| 377 |
</th> |
| 378 |
<th style="margin: 0; padding: 0; font-weight: 900; vertical-align: middle; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;"> |
| 379 |
Weight |
| 380 |
</th> |
| 381 |
</tr> |
| 382 |
</thead> |
| 383 |
<tbody> |
| 384 |
<tr> |
| 385 |
<td style="margin: 0; padding: 0; text-align: left;"> |
| 386 |
Mazda RX4 |
| 387 |
</td> |
| 388 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 389 |
Manual |
| 390 |
</td> |
| 391 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 392 |
21 |
| 393 |
</td> |
| 394 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 395 |
2.62 |
| 396 |
</td> |
| 397 |
</tr> |
| 398 |
<tr> |
| 399 |
<td style="margin: 0; padding: 0; text-align: left;"> |
| 400 |
Mazda RX4 Wag |
| 401 |
</td> |
| 402 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 403 |
Manual |
| 404 |
</td> |
| 405 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 406 |
21 |
| 407 |
</td> |
| 408 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 409 |
2.875 |
| 410 |
</td> |
| 411 |
</tr> |
| 412 |
<tr> |
| 413 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: left;"> |
| 414 |
Datsun 710 |
| 415 |
</td> |
| 416 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: center;"> |
| 417 |
Manual |
| 418 |
</td> |
| 419 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: center;"> |
| 420 |
22.8 |
| 421 |
</td> |
| 422 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: center;"> |
| 423 |
2.32 |
| 424 |
</td> |
| 425 |
</tr> |
| 426 |
</tbody> |
| 427 |
</table> |
| 428 |
<p>doesn’t look visually that great, instead we would prefer to have |
| 429 |
something like this:</p> |
| 430 |
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span> |
| 431 |
<span id="cb2-2"><a href="#cb2-2" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">3</span>) <span class="sc">%>%</span> </span> |
| 432 |
<span id="cb2-3"><a href="#cb2-3" tabindex="-1"></a> <span class="fu">select</span>(<span class="at">Transmission =</span> am, <span class="at">Gas =</span> mpg, <span class="at">Weight =</span> wt) <span class="sc">%>%</span> </span> |
| 433 |
<span id="cb2-4"><a href="#cb2-4" tabindex="-1"></a> <span class="fu">txtRound</span>(<span class="at">digits =</span> <span class="dv">1</span>) <span class="sc">%>%</span> </span> |
| 434 |
<span id="cb2-5"><a href="#cb2-5" tabindex="-1"></a> <span class="fu">htmlTable</span>()</span></code></pre></div> |
| 435 |
<table class="gmisc_table" style="border-collapse: collapse; margin-top: 1em; margin-bottom: 1em;"> |
| 436 |
<thead> |
| 437 |
<tr> |
| 438 |
<th style="border-bottom: 1px solid grey; border-top: 2px solid grey;"> |
| 439 |
</th> |
| 440 |
<th style="margin: 0; padding: 0; font-weight: 900; vertical-align: middle; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;"> |
| 441 |
Transmission |
| 442 |
</th> |
| 443 |
<th style="margin: 0; padding: 0; font-weight: 900; vertical-align: middle; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;"> |
| 444 |
Gas |
| 445 |
</th> |
| 446 |
<th style="margin: 0; padding: 0; font-weight: 900; vertical-align: middle; border-bottom: 1px solid grey; border-top: 2px solid grey; text-align: center;"> |
| 447 |
Weight |
| 448 |
</th> |
| 449 |
</tr> |
| 450 |
</thead> |
| 451 |
<tbody> |
| 452 |
<tr> |
| 453 |
<td style="margin: 0; padding: 0; text-align: left;"> |
| 454 |
Mazda RX4 |
| 455 |
</td> |
| 456 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 457 |
Manual |
| 458 |
</td> |
| 459 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 460 |
21.0 |
| 461 |
</td> |
| 462 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 463 |
2.6 |
| 464 |
</td> |
| 465 |
</tr> |
| 466 |
<tr> |
| 467 |
<td style="margin: 0; padding: 0; text-align: left;"> |
| 468 |
Mazda RX4 Wag |
| 469 |
</td> |
| 470 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 471 |
Manual |
| 472 |
</td> |
| 473 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 474 |
21.0 |
| 475 |
</td> |
| 476 |
<td style="margin: 0; padding: 0; text-align: center;"> |
| 477 |
2.9 |
| 478 |
</td> |
| 479 |
</tr> |
| 480 |
<tr> |
| 481 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: left;"> |
| 482 |
Datsun 710 |
| 483 |
</td> |
| 484 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: center;"> |
| 485 |
Manual |
| 486 |
</td> |
| 487 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: center;"> |
| 488 |
22.8 |
| 489 |
</td> |
| 490 |
<td style="margin: 0; padding: 0; border-bottom: 2px solid grey; text-align: center;"> |
| 491 |
2.3 |
| 492 |
</td> |
| 493 |
</tr> |
| 494 |
</tbody> |
| 495 |
</table> |
| 496 |
<div id="singlevector-values" class="section level3"> |
| 497 |
<h3>Single/vector values</h3> |
| 498 |
<p>At the core of the <code>txtRound</code> is the single/vector value |
| 499 |
conversion:</p> |
| 500 |
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" tabindex="-1"></a><span class="fu">txtRound</span>(<span class="fu">c</span>(<span class="dv">1</span>, <span class="fl">1.1034</span>), <span class="at">digits =</span> <span class="dv">2</span>)</span></code></pre></div> |
| 501 |
<pre><code>## [1] "1.00" "1.10"</code></pre> |
| 502 |
<div class="sourceCode" id="cb5"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" tabindex="-1"></a><span class="co"># Use a character to convert</span></span> |
| 503 |
<span id="cb5-2"><a href="#cb5-2" tabindex="-1"></a><span class="fu">txtRound</span>(<span class="st">"1.2333"</span>, <span class="at">digits =</span> <span class="dv">2</span>)</span></code></pre></div> |
| 504 |
<pre><code>## [1] "1.23"</code></pre> |
| 505 |
<p>If you have some values that need thousand separation you can also |
| 506 |
add <code>txtInt_args</code>.</p> |
| 507 |
<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="co"># Large numbers can be combined with the txtInt option</span></span> |
| 508 |
<span id="cb7-2"><a href="#cb7-2" tabindex="-1"></a><span class="fu">txtRound</span>(<span class="fl">12345.12</span>, <span class="at">digits =</span> <span class="dv">1</span>, <span class="at">txtInt_args =</span> <span class="cn">TRUE</span>)</span></code></pre></div> |
| 509 |
<pre><code>## [1] "12,345.1"</code></pre> |
| 510 |
<div class="sourceCode" id="cb9"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" tabindex="-1"></a><span class="fu">txtRound</span>(<span class="fl">12345.12</span>, <span class="at">digits =</span> <span class="dv">1</span>, <span class="at">txtInt_args =</span> <span class="fu">list</span>(<span class="at">language =</span> <span class="st">"se"</span>, <span class="at">html =</span> <span class="cn">FALSE</span>))</span></code></pre></div> |
| 511 |
<pre><code>## [1] "12 345.1"</code></pre> |
| 512 |
</div> |
| 513 |
<div id="data-frames" class="section level3"> |
| 514 |
<h3>Data frames</h3> |
| 515 |
<p>As seen in the introduction we can use data frames for input. We can |
| 516 |
here rename the converted columns:</p> |
| 517 |
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span> |
| 518 |
<span id="cb11-2"><a href="#cb11-2" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">3</span>) <span class="sc">%>%</span> </span> |
| 519 |
<span id="cb11-3"><a href="#cb11-3" tabindex="-1"></a> <span class="fu">select</span>(mpg, wt) <span class="sc">%>%</span> </span> |
| 520 |
<span id="cb11-4"><a href="#cb11-4" tabindex="-1"></a> <span class="fu">txtRound</span>(mpg, <span class="at">wt_txt =</span> wt, <span class="at">digits =</span> <span class="dv">1</span>)</span></code></pre></div> |
| 521 |
<pre><code>## mpg wt wt_txt |
| 522 |
## Mazda RX4 21.0 2.620 2.6 |
| 523 |
## Mazda RX4 Wag 21.0 2.875 2.9 |
| 524 |
## Datsun 710 22.8 2.320 2.3</code></pre> |
| 525 |
<p>And we can specify the number of decimals that we’re interested in |
| 526 |
per column:</p> |
| 527 |
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" tabindex="-1"></a>mtcars <span class="sc">%>%</span> </span> |
| 528 |
<span id="cb13-2"><a href="#cb13-2" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">3</span>) <span class="sc">%>%</span> </span> |
| 529 |
<span id="cb13-3"><a href="#cb13-3" tabindex="-1"></a> <span class="fu">select</span>(mpg, qsec, wt) <span class="sc">%>%</span> </span> |
| 530 |
<span id="cb13-4"><a href="#cb13-4" tabindex="-1"></a> <span class="fu">txtRound</span>(<span class="at">digits =</span> <span class="fu">list</span>(<span class="at">wt =</span> <span class="dv">2</span>, <span class="at">.default =</span> <span class="dv">1</span>))</span></code></pre></div> |
| 531 |
<pre><code>## mpg qsec wt |
| 532 |
## Mazda RX4 21.0 16.5 2.62 |
| 533 |
## Mazda RX4 Wag 21.0 17.0 2.88 |
| 534 |
## Datsun 710 22.8 18.6 2.32</code></pre> |
| 535 |
</div> |
| 536 |
<div id="matrix" class="section level3"> |
| 537 |
<h3>Matrix</h3> |
| 538 |
<p>We can also feed a matrix into the <code>txtRound</code>:</p> |
| 539 |
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" tabindex="-1"></a>mtcars_matrix <span class="ot"><-</span> mtcars <span class="sc">%>%</span> </span> |
| 540 |
<span id="cb15-2"><a href="#cb15-2" tabindex="-1"></a> <span class="fu">select</span>(mpg, qsec, wt) <span class="sc">%>%</span> </span> |
| 541 |
<span id="cb15-3"><a href="#cb15-3" tabindex="-1"></a> <span class="fu">head</span>(<span class="dv">3</span>) <span class="sc">%>%</span> </span> |
| 542 |
<span id="cb15-4"><a href="#cb15-4" tabindex="-1"></a> <span class="fu">as.matrix</span>()</span> |
| 543 |
<span id="cb15-5"><a href="#cb15-5" tabindex="-1"></a></span> |
| 544 |
<span id="cb15-6"><a href="#cb15-6" tabindex="-1"></a>mtcars_matrix <span class="sc">%>%</span> </span> |
| 545 |
<span id="cb15-7"><a href="#cb15-7" tabindex="-1"></a> <span class="fu">txtRound</span>(<span class="at">digits =</span> <span class="dv">1</span>)</span></code></pre></div> |
| 546 |
<pre><code>## mpg qsec wt |
| 547 |
## Mazda RX4 "21.0" "16.5" "2.6" |
| 548 |
## Mazda RX4 Wag "21.0" "17.0" "2.9" |
| 549 |
## Datsun 710 "22.8" "18.6" "2.3"</code></pre> |
| 550 |
<p>Here we have some options of excluding columns/rows using regular |
| 551 |
expressions:</p> |
| 552 |
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" tabindex="-1"></a>mtcars_matrix <span class="sc">%>%</span> </span> |
| 553 |
<span id="cb17-2"><a href="#cb17-2" tabindex="-1"></a> <span class="fu">txtRound</span>(<span class="at">excl.cols =</span> <span class="st">"^wt$"</span>,</span> |
| 554 |
<span id="cb17-3"><a href="#cb17-3" tabindex="-1"></a> <span class="at">excl.rows =</span> <span class="st">"^Mazda RX4$"</span>,</span> |
| 555 |
<span id="cb17-4"><a href="#cb17-4" tabindex="-1"></a> <span class="at">digits =</span> <span class="dv">1</span>)</span></code></pre></div> |
| 556 |
<pre><code>## mpg qsec wt |
| 557 |
## Mazda RX4 "21" "16.46" "2.62" |
| 558 |
## Mazda RX4 Wag "21.0" "17.0" "2.875" |
| 559 |
## Datsun 710 "22.8" "18.6" "2.32"</code></pre> |
| 560 |
<p>Similarly to the data.frame we can use the same syntax to pick column |
| 561 |
specific digits:</p> |
| 562 |
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" tabindex="-1"></a>mtcars_matrix <span class="sc">%>%</span> </span> |
| 563 |
<span id="cb19-2"><a href="#cb19-2" tabindex="-1"></a> <span class="fu">txtRound</span>(<span class="at">digits =</span> <span class="fu">list</span>(<span class="at">mpg =</span> <span class="dv">0</span>, <span class="at">wt =</span> <span class="dv">2</span>, <span class="at">.default =</span> <span class="dv">1</span>))</span></code></pre></div> |
| 564 |
<pre><code>## mpg qsec wt |
| 565 |
## Mazda RX4 "21" "16.5" "2.62" |
| 566 |
## Mazda RX4 Wag "21" "17.0" "2.88" |
| 567 |
## Datsun 710 "23" "18.6" "2.32"</code></pre> |
| 568 |
</div> |
| 569 |
</div> |
| 570 |
<div id="txtint" class="section level2"> |
| 571 |
<h2>txtInt</h2> |
| 572 |
<p>While scientific format is useful if familiar with the syntax it can |
| 573 |
be difficult to grasp for scholars with a less mathematical background. |
| 574 |
Therefore the thousand separator style can be quite useful, also known |
| 575 |
as <a href="https://en.wikipedia.org/wiki/Decimal_separator#Digit_grouping">digital |
| 576 |
grouping</a>:</p> |
| 577 |
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" tabindex="-1"></a><span class="fu">txtInt</span>(<span class="fl">1e7</span>)</span></code></pre></div> |
| 578 |
<pre><code>## [1] "10,000,000"</code></pre> |
| 579 |
<p>As Swedish and many other languages rely on space (SI-standard) we |
| 580 |
can specify language as a parameter. Note that as we don’t want to have |
| 581 |
line breaks within a digit we can use <a href="https://en.wikipedia.org/wiki/Non-breaking_space">non-breaking |
| 582 |
space</a> for keeping the number intact (the html-code is |
| 583 |
<code>&nbsp;</code>):</p> |
| 584 |
<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">txtInt</span>(<span class="fl">1e7</span>, <span class="at">language =</span> <span class="st">"SI"</span>, <span class="at">html =</span> <span class="cn">FALSE</span>)</span></code></pre></div> |
| 585 |
<pre><code>## [1] "10 000 000"</code></pre> |
| 586 |
<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">txtInt</span>(<span class="fl">1e7</span>, <span class="at">language =</span> <span class="st">"SI"</span>, <span class="at">html =</span> <span class="cn">TRUE</span>)</span></code></pre></div> |
| 587 |
<pre><code>## [1] "10&nbsp;000&nbsp;000"</code></pre> |
| 588 |
<p>Note that there are the option <code>htmlTable.language</code> and |
| 589 |
<code>htmlTable.html</code> that you can use for the input of these |
| 590 |
parameters.</p> |
| 591 |
</div> |
| 592 |
<div id="txtpval" class="section level2"> |
| 593 |
<h2>txtPval</h2> |
| 594 |
<p>The p-value is perhaps the most controversial of statistical output, |
| 595 |
nevertheless it is still needed and used correctly it has it’s use. |
| 596 |
P-values are frequently rounded as the decimals are not as important. |
| 597 |
The <code>txtPval</code> is a convenient function with some defaults |
| 598 |
that correspond to typical uses in medical publications.</p> |
| 599 |
<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">txtPval</span>(<span class="fu">c</span>(<span class="fl">0.1233213</span>, <span class="fl">0.035</span>, <span class="fl">0.001</span>, <span class="fl">0.000001</span>), <span class="at">html =</span> <span class="cn">FALSE</span>)</span></code></pre></div> |
| 600 |
<pre><code>## [1] "0.12" "0.035" "0.001" "< 0.0001"</code></pre> |
| 601 |
<div class="sourceCode" id="cb29"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb29-1"><a href="#cb29-1" tabindex="-1"></a><span class="co"># The < sign is less-than in html code '&lt;'</span></span> |
| 602 |
<span id="cb29-2"><a href="#cb29-2" tabindex="-1"></a><span class="fu">txtPval</span>(<span class="fu">c</span>(<span class="fl">0.05</span>, <span class="fl">0.001</span>, <span class="fl">0.000001</span>), <span class="at">html =</span> <span class="cn">TRUE</span>)</span></code></pre></div> |
| 603 |
<pre><code>## [1] "0.050" "0.001" "&lt; 0.0001"</code></pre> |
| 604 |
</div> |
| 605 |
<div id="txtmergelines" class="section level2"> |
| 606 |
<h2>txtMergeLines</h2> |
| 607 |
<p>In html we indicate new line using <em><br /></em> while the |
| 608 |
latex style uses <em>hbox</em>. To help with these two there is the |
| 609 |
<code>txtMergeLines</code> that merges lines into one properly formatted |
| 610 |
unit:</p> |
| 611 |
<div class="sourceCode" id="cb31"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb31-1"><a href="#cb31-1" tabindex="-1"></a><span class="fu">txtMergeLines</span>(<span class="st">"Line 1"</span>,</span> |
| 612 |
<span id="cb31-2"><a href="#cb31-2" tabindex="-1"></a> <span class="st">"Line 2"</span>,</span> |
| 613 |
<span id="cb31-3"><a href="#cb31-3" tabindex="-1"></a> <span class="st">"Line 3"</span>)</span></code></pre></div> |
| 614 |
<p>Line 1<br> Line 2<br> Line 3</p> |
| 615 |
<p>Note that you can also use a single multi-line string:</p> |
| 616 |
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb32-1"><a href="#cb32-1" tabindex="-1"></a><span class="fu">txtMergeLines</span>(<span class="st">"Line 1</span></span> |
| 617 |
<span id="cb32-2"><a href="#cb32-2" tabindex="-1"></a><span class="st"> Line 2</span></span> |
| 618 |
<span id="cb32-3"><a href="#cb32-3" tabindex="-1"></a><span class="st"> Line 3"</span>)</span></code></pre></div> |
| 619 |
<p>Line 1<br> Line 2<br> Line 3</p> |
| 620 |
<div class="sourceCode" id="cb33"><pre class="sourceCode r"><code class="sourceCode r"><span id="cb33-1"><a href="#cb33-1" tabindex="-1"></a><span class="fu">txtMergeLines</span>(<span class="st">"Line 1</span></span> |
| 621 |
<span id="cb33-2"><a href="#cb33-2" tabindex="-1"></a><span class="st"> Line 2</span></span> |
| 622 |
<span id="cb33-3"><a href="#cb33-3" tabindex="-1"></a><span class="st"> Line 3"</span>,</span> |
| 623 |
<span id="cb33-4"><a href="#cb33-4" tabindex="-1"></a> <span class="at">html =</span> <span class="cn">FALSE</span>)</span></code></pre></div> |
| 624 |
<pre><code>## [1] "\\vbox{\\hbox{\\strut Line 1}\\hbox{\\strut Line 2}\\hbox{\\strut Line 3}}"</code></pre> |
| 625 |
</div> |
| 626 |
</div> |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
<script> |
| 635 |
(function () { |
| 636 |
var script = document.createElement("script"); |
| 637 |
script.type = "text/javascript"; |
| 638 |
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"; |
| 639 |
document.getElementsByTagName("head")[0].appendChild(script); |
| 640 |
})(); |
| 641 |
</script> |
| 642 |
|
| 643 |
</body> |
| 644 |
</html> |