AMR/vignettes/freq.html

345 lines
28 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="author" content="Matthijs S. Berends" />
<title>Creating Frequency Tables</title>
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link href="data:text/css;charset=utf-8,body%20%7B%0Abackground%2Dcolor%3A%20%23fff%3B%0Amargin%3A%201em%20auto%3B%0Amax%2Dwidth%3A%20700px%3B%0Aoverflow%3A%20visible%3B%0Apadding%2Dleft%3A%202em%3B%0Apadding%2Dright%3A%202em%3B%0Afont%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0Afont%2Dsize%3A%2014px%3B%0Aline%2Dheight%3A%201%2E35%3B%0A%7D%0A%23header%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0A%23TOC%20%7B%0Aclear%3A%20both%3B%0Amargin%3A%200%200%2010px%2010px%3B%0Apadding%3A%204px%3B%0Awidth%3A%20400px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Aborder%2Dradius%3A%205px%3B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Afont%2Dsize%3A%2013px%3B%0Aline%2Dheight%3A%201%2E3%3B%0A%7D%0A%23TOC%20%2Etoctitle%20%7B%0Afont%2Dweight%3A%20bold%3B%0Afont%2Dsize%3A%2015px%3B%0Amargin%2Dleft%3A%205px%3B%0A%7D%0A%23TOC%20ul%20%7B%0Apadding%2Dleft%3A%2040px%3B%0Amargin%2Dleft%3A%20%2D1%2E5em%3B%0Amargin%2Dtop%3A%205px%3B%0Amargin%2Dbottom%3A%205px%3B%0A%7D%0A%23TOC%20ul%20ul%20%7B%0Amargin%2Dleft%3A%20%2D2em%3B%0A%7D%0A%23TOC%20li%20%7B%0Aline%2Dheight%3A%2016px%3B%0A%7D%0Atable%20%7B%0Amargin%3A%201em%20auto%3B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dcolor%3A%20%23DDDDDD%3B%0Aborder%2Dstyle%3A%20outset%3B%0Aborder%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0Aborder%2Dwidth%3A%202px%3B%0Apadding%3A%205px%3B%0Aborder%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0Aborder%2Dwidth%3A%201px%3B%0Aborder%2Dstyle%3A%20inset%3B%0Aline%2Dheight%3A%2018px%3B%0Apadding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0Aborder%2Dleft%2Dstyle%3A%20none%3B%0Aborder%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Ap%20%7B%0Amargin%3A%200%2E5em%200%3B%0A%7D%0Ablockquote%20%7B%0Abackground%2Dcolor%3A%20%23f6f6f6%3B%0Apadding%3A%200%2E25em%200%2E75em%3B%0A%7D%0Ahr%20%7B%0Aborder%2Dstyle%3A%20solid%3B%0Aborder%3A%20none%3B%0Aborder%2Dtop%3A%201px%20solid%20%23777%3B%0Amargin%3A%2028px%200%3B%0A%7D%0Adl%20%7B%0Amargin%2Dleft%3A%200%3B%0A%7D%0Adl%20dd%20%7B%0Amargin%2Dbottom%3A%2013px%3B%0Amargin%2Dleft%3A%2013px%3B%0A%7D%0Adl%20dt%20%7B%0Afont%2Dweight%3A%20bold%3B%0A%7D%0Aul%20%7B%0Amargin%2Dtop%3A%200%3B%0A%7D%0Aul%20li%20%7B%0Alist%2Dstyle%3A%20circle%20outside%3B%0A%7D%0Aul%20ul%20%7B%0Amargin%2Dbottom%3A%200%3B%0A%7D%0Apre%2C%20code%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0Aborder%2Dradius%3A%203px%3B%0Acolor%3A%20%23333%3B%0Awhite%2Dspace%3A%20pre%2Dwrap%3B%20%0A%7D%0Apre%20%7B%0Aborder%2Dradius%3A%203px%3B%0Amargin%3A%205px%200px%2010px%200px%3B%0Apadding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0Abackground%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0Acode%20%7B%0Afont%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0Afont%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0Apadding%3A%202px%200px%3B%0A%7D%0Adiv%2Efigure%20%7B%0Atext%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0Abackground%2Dcolor%3A%20%23FFFFFF%3B%0Apadding%3A%202px%3B%0Aborder%3A%201px%20solid%20%23DDDDDD%3B%0Aborder%2Dradius%3A%203px%3B%0Aborder%3A%201px%20solid%20%23CCCCCC%3B%0Amargin%3A%200%205px%3B%0A%7D%0Ah1%20%7B%0Amargin%2Dtop%3A%200%3B%0Afont%2Dsize%3A%2035px%3B%0Aline%2Dheight%3A%2040px%3B%0A%7D%0Ah2%20%7B%0Aborder%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Apadding%2Dbottom%3A%202px%3B%0Afont%2Dsize%3A%20145%25%3B%0A%7D%0Ah3%20%7B%0Aborder%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0Apadding%2Dtop%3A%2010px%3B%0Afont%2Dsize%3A%20120%25%3B%0A%7D%0Ah4%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0Amargin%2Dleft%3A%208px%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Ah5%2C%20h6%20%7B%0Aborder%2Dbottom%3A%201px%20solid%20%23ccc%3B%0Afont%2Dsize%3A%20105%25%3B%0A%7D%0Aa%20%7B%0Acolor%3A%20%230033dd%3B%0Atext%2Ddecoration%3A%20none%3B%0A%7D%0Aa%3Ahover%20%7B%0Acolor%3A%20%236666ff%3B%20%7D%0Aa%3Avisited%20%7B%0Acolor%3A%20%23800080%3B%20%7D%0Aa%3Avisited%3Ahover%20%7B%0Acolor%3A%20%23BB00BB%3B%20%7D%0Aa%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0Aa%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0Atext%2Ddecoration%3A%20underline%3B%20%7D%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%20code%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%0A" rel="stylesheet" type="text/css" />
</head>
<body>
<h1 class="title toc-ignore">Creating Frequency Tables</h1>
<h4 class="author"><em>Matthijs S. Berends</em></h4>
<div id="TOC">
<ul>
<li><a href="#introduction">Introduction</a></li>
<li><a href="#frequencies-of-one-variable">Frequencies of one variable</a></li>
<li><a href="#frequencies-of-more-than-one-variable">Frequencies of more than one variable</a></li>
<li><a href="#frequencies-of-numeric-values">Frequencies of numeric values</a></li>
<li><a href="#frequencies-of-factors">Frequencies of factors</a></li>
<li><a href="#frequencies-of-dates">Frequencies of dates</a></li>
<li><a href="#additional-parameters">Additional parameters</a><ul>
<li><a href="#parameter-na.rm">Parameter <code>na.rm</code></a></li>
<li><a href="#parameter-markdown">Parameter <code>markdown</code></a></li>
<li><a href="#parameter-as.data.frame">Parameter <code>as.data.frame</code></a></li>
</ul></li>
</ul>
</div>
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>Frequency tables (or frequency distributions) are summaries of the distribution of values in a sample. With the <code>freq</code> function, you can create univariate frequency tables. Multiple variables will be pasted into one variable, so it forces a univariate distribution. We take the <code>septic_patients</code> dataset (included in this AMR package) as example.</p>
</div>
<div id="frequencies-of-one-variable" class="section level2">
<h2>Frequencies of one variable</h2>
<p>To only show and quickly review the content of one variable, you can just select this variable in various ways. Lets say we want to get the frequencies of the <code>sex</code> variable of the <code>septic_patients</code> dataset:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># # just using base R</span>
<span class="kw">freq</span>(septic_patients$sex)
<span class="co"># # using base R to select the variable and pass it on with a pipe</span>
septic_patients$sex %&gt;%<span class="st"> </span><span class="kw">freq</span>()
<span class="co"># # do it all with pipes, using the `select` function of the dplyr package</span>
septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(sex) %&gt;%
<span class="st"> </span><span class="kw">freq</span>()</code></pre></div>
<p>This will all lead to the following table:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">freq</span>(septic_patients$sex)
<span class="co"># Class: character</span>
<span class="co"># Length: 2000 (of which NA: 0 = 0.0%)</span>
<span class="co"># Unique: 2</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent</span>
<span class="co"># ----- ------ -------- ----------- -------------</span>
<span class="co"># M 1112 55.6% 1112 55.6%</span>
<span class="co"># F 888 44.4% 2000 100.0%</span></code></pre></div>
<p>This immediately shows the class of the variable, its length and availability (i.e. the amount of <code>NA</code>), the amount of unique values and (most importantly) that among septic patients men are more prevalent than women.</p>
</div>
<div id="frequencies-of-more-than-one-variable" class="section level2">
<h2>Frequencies of more than one variable</h2>
<p>Multiple variables will be pasted into one variable to review individual cases, keeping a univariate frequency table.</p>
<p>For illustration, we could add some more variables to the <code>septic_patients</code> dataset to learn about bacterial properties:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">my_patients &lt;-<span class="st"> </span>septic_patients %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">left_join_microorganisms</span>()</code></pre></div>
<p>Now all variables of the <code>microorganisms</code> dataset have been joined to the <code>septic_patients</code> dataset. The <code>microorganisms</code> dataset consists of the following variables:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">colnames</span>(microorganisms)
<span class="co"># [1] &quot;bactid&quot; &quot;bactsys&quot; &quot;family&quot; &quot;genus&quot; </span>
<span class="co"># [5] &quot;species&quot; &quot;subspecies&quot; &quot;fullname&quot; &quot;type&quot; </span>
<span class="co"># [9] &quot;gramstain&quot; &quot;aerobic&quot; &quot;type_nl&quot; &quot;gramstain_nl&quot;</span></code></pre></div>
<p>If we compare the dimensions between the old and new dataset, we can see that these 11 variables were added:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">dim</span>(septic_patients)
<span class="co"># [1] 2000 47</span>
<span class="kw">dim</span>(my_patients)
<span class="co"># [1] 2000 58</span></code></pre></div>
<p>So now the <code>genus</code> and <code>species</code> variables are available. A frequency table of these combined variables can be created like this:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">my_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(genus, species) %&gt;%
<span class="st"> </span><span class="kw">freq</span>()
<span class="co"># Columns: 2</span>
<span class="co"># Length: 2000 (of which NA: 0 = 0.0%)</span>
<span class="co"># Unique: 137</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent</span>
<span class="co"># ---------------------------------- ------ -------- ----------- -------------</span>
<span class="co"># Escherichia coli 485 24.2% 485 24.2%</span>
<span class="co"># Staphylococcus coagulase negatief 297 14.8% 782 39.1%</span>
<span class="co"># Staphylococcus aureus 200 10.0% 982 49.1%</span>
<span class="co"># Staphylococcus epidermidis 150 7.5% 1132 56.6%</span>
<span class="co"># Streptococcus pneumoniae 97 4.9% 1229 61.5%</span>
<span class="co"># Staphylococcus hominis 67 3.4% 1296 64.8%</span>
<span class="co"># Klebsiella pneumoniae 65 3.2% 1361 68.0%</span>
<span class="co"># Enterococcus faecalis 44 2.2% 1405 70.2%</span>
<span class="co"># Proteus mirabilis 33 1.7% 1438 71.9%</span>
<span class="co"># Pseudomonas aeruginosa 31 1.6% 1469 73.5%</span>
<span class="co"># Streptococcus pyogenes 30 1.5% 1499 75.0%</span>
<span class="co"># Enterococcus faecium 27 1.4% 1526 76.3%</span>
<span class="co"># Bacteroides fragilis 26 1.3% 1552 77.6%</span>
<span class="co"># Enterobacter cloacae 25 1.2% 1577 78.8%</span>
<span class="co"># Klebsiella oxytoca 23 1.1% 1600 80.0%</span>
<span class="co"># ... and 122 more (n = 400; 20.0%). Use `nmax` to show more or less rows.</span></code></pre></div>
</div>
<div id="frequencies-of-numeric-values" class="section level2">
<h2>Frequencies of numeric values</h2>
<p>Frequency tables can be created of any input.</p>
<p>In case of numeric values (like integers, doubles, etc.) additional descriptive statistics will be calculated and shown into the header:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># # get age distribution of unique patients</span>
septic_patients %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">distinct</span>(patient_id, <span class="dt">.keep_all =</span> <span class="ot">TRUE</span>) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">select</span>(age) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>(<span class="dt">nmax =</span> <span class="dv">5</span>)
<span class="co"># Class: integer</span>
<span class="co"># Length: 1920 (of which NA: 0 = 0.0%)</span>
<span class="co"># Unique: 94</span>
<span class="co"># </span>
<span class="co"># Mean: 68</span>
<span class="co"># Std. dev.: 18 (CV: 0.27)</span>
<span class="co"># Five-Num: 0 | 61 | 72 | 80 | 101 (CQV: 0.13)</span>
<span class="co"># Outliers: 94 (unique: 26)</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent</span>
<span class="co"># ----- ------ -------- ----------- -------------</span>
<span class="co"># 0 34 1.8% 34 1.8%</span>
<span class="co"># 1 5 0.3% 39 2.0%</span>
<span class="co"># 2 5 0.3% 44 2.3%</span>
<span class="co"># 3 2 0.1% 46 2.4%</span>
<span class="co"># 4 1 0.1% 47 2.4%</span>
<span class="co"># ... and 89 more (n = 1873; 97.6%).</span></code></pre></div>
<p>So the following properties are determined, where <code>NA</code> values are always ignored:</p>
<ul>
<li><p><strong>Mean</strong></p></li>
<li><p><strong>Standard deviation</strong></p></li>
<li><p><strong>Coefficient of variation</strong> (CV), the standard deviation divided by the mean</p></li>
<li><p><strong>Five numbers of Tukey</strong> (min, Q1, median, Q3, max)</p></li>
<li><p><strong>Coefficient of quartile variation</strong> (CQV, sometimes called coefficient of dispersion), calculated as (Q3 - Q1) / (Q3 + Q1) using quantile with <code>type = 6</code> as quantile algorithm to comply with SPSS standards</p></li>
<li><p><strong>Outliers</strong> (total count and unique count)</p></li>
</ul>
<p>So for example, the above frequency table quickly shows the median age of patients being 72.</p>
</div>
<div id="frequencies-of-factors" class="section level2">
<h2>Frequencies of factors</h2>
<p>Frequencies of factors will be sorted on factor level instead of item count by default. This can be changed with the <code>sort.count</code> parameter. Frequency tables of factors always show the factor level as an additional last column.</p>
<p><code>sort.count</code> is <code>TRUE</code> by default, except for factors. Compare this default behaviour:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(hospital_id) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>()
<span class="co"># Class: factor</span>
<span class="co"># Length: 2000 (of which NA: 0 = 0.0%)</span>
<span class="co"># Unique: 5</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent (Factor Level)</span>
<span class="co"># ----- ------ -------- ----------- ------------- ---------------</span>
<span class="co"># A 233 11.7% 233 11.7% 1</span>
<span class="co"># B 583 29.1% 816 40.8% 2</span>
<span class="co"># C 221 11.1% 1037 51.8% 3</span>
<span class="co"># D 650 32.5% 1687 84.4% 4</span>
<span class="co"># E 313 15.7% 2000 100.0% 5</span></code></pre></div>
<p>To this, where items are now sorted on item count:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(hospital_id) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>(<span class="dt">sort.count =</span> <span class="ot">TRUE</span>)
<span class="co"># Class: factor</span>
<span class="co"># Length: 2000 (of which NA: 0 = 0.0%)</span>
<span class="co"># Unique: 5</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent (Factor Level)</span>
<span class="co"># ----- ------ -------- ----------- ------------- ---------------</span>
<span class="co"># D 650 32.5% 650 32.5% 4</span>
<span class="co"># B 583 29.1% 1233 61.7% 2</span>
<span class="co"># E 313 15.7% 1546 77.3% 5</span>
<span class="co"># A 233 11.7% 1779 88.9% 1</span>
<span class="co"># C 221 11.1% 2000 100.0% 3</span></code></pre></div>
<p>All classes will be printed into the header. Variables with the new <code>rsi</code> class of this AMR package are actually ordered factors and have three classes (look at <code>Class</code> in the header):</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(amox) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>()
<span class="co"># Class: factor &gt; ordered &gt; rsi</span>
<span class="co"># Length: 2000 (of which NA: 678 = 33.9%)</span>
<span class="co"># Unique: 3</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent (Factor Level)</span>
<span class="co"># ----- ------ -------- ----------- ------------- ---------------</span>
<span class="co"># S 561 42.4% 561 42.4% 1</span>
<span class="co"># I 49 3.7% 610 46.1% 2</span>
<span class="co"># R 712 53.9% 1322 100.0% 3</span></code></pre></div>
</div>
<div id="frequencies-of-dates" class="section level2">
<h2>Frequencies of dates</h2>
<p>Frequencies of dates will show the oldest and newest date in the data, and the amount of days between them:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(date) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>(<span class="dt">nmax =</span> <span class="dv">5</span>)
<span class="co"># Class: Date</span>
<span class="co"># Length: 2000 (of which NA: 0 = 0.0%)</span>
<span class="co"># Unique: 1662</span>
<span class="co"># </span>
<span class="co"># Oldest: 2 januari 2001</span>
<span class="co"># Newest: 18 oktober 2017 (+6133)</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent</span>
<span class="co"># ----------- ------ -------- ----------- -------------</span>
<span class="co"># 2008-12-24 5 0.2% 5 0.2%</span>
<span class="co"># 2010-12-10 4 0.2% 9 0.4%</span>
<span class="co"># 2011-03-03 4 0.2% 13 0.6%</span>
<span class="co"># 2013-06-24 4 0.2% 17 0.8%</span>
<span class="co"># 2017-09-01 4 0.2% 21 1.1%</span>
<span class="co"># ... and 1657 more (n = 1979; 99.0%).</span></code></pre></div>
</div>
<div id="additional-parameters" class="section level2">
<h2>Additional parameters</h2>
<div id="parameter-na.rm" class="section level3">
<h3>Parameter <code>na.rm</code></h3>
<p>With the <code>na.rm</code> parameter (defaults to <code>TRUE</code>, but they will always be shown into the header), you can include <code>NA</code> values in the frequency table:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(amox) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>(<span class="dt">na.rm =</span> <span class="ot">FALSE</span>)
<span class="co"># Class: factor &gt; ordered &gt; rsi</span>
<span class="co"># Length: 2678 (of which NA: 678 = 25.3%)</span>
<span class="co"># Unique: 4</span>
<span class="co"># </span>
<span class="co"># Item Count Percent Cum. Count Cum. Percent (Factor Level)</span>
<span class="co"># ----- ------ -------- ----------- ------------- ---------------</span>
<span class="co"># S 561 28.1% 561 28.1% 1</span>
<span class="co"># I 49 2.5% 610 30.5% 2</span>
<span class="co"># R 712 35.6% 1322 66.1% 3</span>
<span class="co"># &lt;NA&gt; 678 33.9% 2000 100.0% &lt;NA&gt;</span></code></pre></div>
</div>
<div id="parameter-markdown" class="section level3">
<h3>Parameter <code>markdown</code></h3>
<p>The <code>markdown</code> parameter can be used in reports created with R Markdown. This will always print all rows:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(hospital_id) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>(<span class="dt">markdown =</span> <span class="ot">TRUE</span>)
<span class="co"># </span>
<span class="co"># Class: factor</span>
<span class="co"># </span>
<span class="co"># Length: 2000 (of which NA: 0 = 0.0%)</span>
<span class="co"># </span>
<span class="co"># Unique: 5</span>
<span class="co"># </span>
<span class="co"># |Item | Count| Percent| Cum. Count| Cum. Percent| (Factor Level)|</span>
<span class="co"># |:----|-----:|-------:|----------:|------------:|--------------:|</span>
<span class="co"># |A | 233| 11.7%| 233| 11.7%| 1|</span>
<span class="co"># |B | 583| 29.1%| 816| 40.8%| 2|</span>
<span class="co"># |C | 221| 11.1%| 1037| 51.8%| 3|</span>
<span class="co"># |D | 650| 32.5%| 1687| 84.4%| 4|</span>
<span class="co"># |E | 313| 15.7%| 2000| 100.0%| 5|</span></code></pre></div>
</div>
<div id="parameter-as.data.frame" class="section level3">
<h3>Parameter <code>as.data.frame</code></h3>
<p>With the <code>as.data.frame</code> parameter you can assign the frequency table to an object, or just print it as a <code>data.frame</code> to the console:</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">my_df &lt;-<span class="st"> </span>septic_patients %&gt;%
<span class="st"> </span><span class="kw">select</span>(hospital_id) %&gt;%<span class="st"> </span>
<span class="st"> </span><span class="kw">freq</span>(<span class="dt">as.data.frame =</span> <span class="ot">TRUE</span>)
my_df
<span class="co"># item count percent cum_count cum_percent factor_level</span>
<span class="co"># 1 A 233 0.1165 233 0.1165 1</span>
<span class="co"># 2 B 583 0.2915 816 0.4080 2</span>
<span class="co"># 3 C 221 0.1105 1037 0.5185 3</span>
<span class="co"># 4 D 650 0.3250 1687 0.8435 4</span>
<span class="co"># 5 E 313 0.1565 2000 1.0000 5</span>
<span class="kw">class</span>(my_df)
<span class="co"># [1] &quot;data.frame&quot;</span></code></pre></div>
<hr />
<p>AMR, (c) 2018, <a href="https://github.com/msberends/AMR" class="uri">https://github.com/msberends/AMR</a></p>
<p>Licensed under the <a href="https://github.com/msberends/AMR/blob/master/LICENSE">GNU General Public License v2.0</a>.</p>
</div>
</div>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>