AMR/docs/reference/freq.html

554 lines
32 KiB
HTML
Raw Normal View History

2018-12-29 22:24:19 +01:00
<!-- Generated by pkgdown: do not edit by hand -->
<!DOCTYPE html>
<html lang="en">
<head>
<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>Frequency table — freq • AMR (for R)</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.3.1/jquery.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
<!-- Bootstrap -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootswatch/3.3.7/flatly/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha256-U5ZEeKfGNOja007MMD3YBI0A3OSZOQbeG6z2f2Y0hu8=" crossorigin="anonymous"></script>
<!-- Font Awesome icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" integrity="sha256-eZrrJcwDc/3uDhsdt61sL2oOBY362qM3lon1gyExkL0=" crossorigin="anonymous" />
<!-- clipboard.js -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.4/clipboard.min.js" integrity="sha256-FiZwavyI2V6+EXO1U+xzLG3IKldpiTFf3153ea9zikQ=" crossorigin="anonymous"></script>
<!-- sticky kit -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/sticky-kit/1.1.3/sticky-kit.min.js" integrity="sha256-c4Rlo1ZozqTPE2RLuvbusY3+SU1pQaJC0TjuhygMipw=" crossorigin="anonymous"></script>
<!-- pkgdown -->
<link href="../pkgdown.css" rel="stylesheet">
<script src="../pkgdown.js"></script>
<!-- docsearch -->
<script src="../docsearch.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/docsearch.js/2.6.1/docsearch.min.css" integrity="sha256-QOSRU/ra9ActyXkIBbiIB144aDBdtvXBcNc3OTNuX/Q=" crossorigin="anonymous" />
<link href="../docsearch.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/mark.js/8.11.1/jquery.mark.min.js" integrity="sha256-4HLtjeVgH0eIB3aZ9mLYF6E8oU5chNdjU6p6rrXpl9U=" crossorigin="anonymous"></script>
<link href="../extra.css" rel="stylesheet">
<script src="../extra.js"></script>
<meta property="og:title" content="Frequency table — freq" />
2019-01-28 11:20:32 +01:00
<meta property="og:description" content="Create a frequency table of a vector with items or a data.frame. Supports quasiquotation and markdown for reports. Best practice is: data %&amp;gt;% freq(var).
2018-12-31 12:03:35 +01:00
top_freq can be used to get the top/bottom n items of a frequency table, with counts as names." />
2018-12-29 22:24:19 +01:00
2019-01-02 23:24:07 +01:00
<meta property="og:image" content="https://msberends.gitlab.io/AMR/logo.png" />
2018-12-29 22:24:19 +01:00
<meta name="twitter:card" content="summary" />
<!-- 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>
<div class="container template-reference-topic">
<header>
<div class="navbar navbar-default 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">AMR (for R)</a>
2019-07-09 11:22:46 +02:00
<span class="version label label-default" data-toggle="tooltip" data-placement="bottom" title="Latest development version">0.7.1.9009</span>
2018-12-29 22:24:19 +01:00
</span>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="../index.html">
<span class="fa fa-home"></span>
Home
</a>
</li>
2019-01-02 23:24:07 +01:00
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="fa fa-question-circle"></span>
How to
2018-12-29 22:24:19 +01:00
2019-01-02 23:24:07 +01:00
<span class="caret"></span>
2018-12-29 22:24:19 +01:00
</a>
2019-01-02 23:24:07 +01:00
<ul class="dropdown-menu" role="menu">
<li>
<a href="../articles/AMR.html">
<span class="fa fa-directions"></span>
Conduct AMR analysis
</a>
</li>
<li>
2019-02-14 10:23:51 +01:00
<a href="../articles/resistance_predict.html">
2019-01-02 23:24:07 +01:00
<span class="fa fa-dice"></span>
Predict antimicrobial resistance
</a>
</li>
2019-05-28 16:50:40 +02:00
<li>
<a href="../articles/MDR.html">
<span class="fa fa-skull-crossbones"></span>
Determine multi-drug resistance (MDR)
</a>
</li>
2019-01-29 00:06:50 +01:00
<li>
<a href="../articles/WHONET.html">
<span class="fa fa-globe-americas"></span>
Work with WHONET data
</a>
</li>
2019-02-14 15:18:17 +01:00
<li>
<a href="../articles/SPSS.html">
<span class="fa fa-file-upload"></span>
Import data from SPSS/SAS/Stata
</a>
</li>
2019-01-02 23:24:07 +01:00
<li>
<a href="../articles/EUCAST.html">
<span class="fa fa-exchange-alt"></span>
Apply EUCAST rules
</a>
</li>
<li>
2019-02-14 10:23:51 +01:00
<a href="../reference/mo_property.html">
2019-01-02 23:24:07 +01:00
<span class="fa fa-bug"></span>
Get properties of a microorganism
</a>
</li>
<li>
2019-05-10 16:44:59 +02:00
<a href="../reference/ab_property.html">
2019-01-02 23:24:07 +01:00
<span class="fa fa-capsules"></span>
Get properties of an antibiotic
</a>
</li>
<li>
<a href="../articles/freq.html">
<span class="fa fa-sort-amount-down"></span>
Create frequency tables
</a>
</li>
2019-01-11 20:37:23 +01:00
<li>
<a href="../articles/benchmarks.html">
<span class="fa fa-shipping-fast"></span>
Other: benchmarks
</a>
</li>
2019-01-02 23:24:07 +01:00
</ul>
2018-12-29 22:24:19 +01:00
</li>
<li>
<a href="../reference/">
<span class="fa fa-book-open"></span>
Manual
</a>
</li>
<li>
<a href="../authors.html">
<span class="fa fa-users"></span>
Authors
</a>
</li>
<li>
<a href="../news/">
<span class="far fa far fa-newspaper"></span>
Changelog
</a>
</li>
2019-01-02 23:24:07 +01:00
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
2018-12-29 22:24:19 +01:00
<a href="https://gitlab.com/msberends/AMR">
<span class="fab fa fab fa-gitlab"></span>
Source Code
</a>
</li>
2019-01-02 23:24:07 +01:00
<li>
2018-12-29 22:24:19 +01:00
<a href="../LICENSE-text.html">
<span class="fa fa-book"></span>
Licence
</a>
</li>
</ul>
<form class="navbar-form navbar-right" role="search">
<div class="form-group">
<input type="search" class="form-control" name="search-input" id="search-input" placeholder="Search..." aria-label="Search for..." autocomplete="off">
</div>
</form>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
</header>
<div class="row">
<div class="col-md-9 contents">
<div class="page-header">
<h1>Frequency table</h1>
<div class="hidden name"><code>freq.Rd</code></div>
</div>
<div class="ref-description">
2019-01-28 11:20:32 +01:00
<p>Create a frequency table of a vector with items or a <code>data.frame</code>. Supports quasiquotation and markdown for reports. Best practice is: <code>data %&gt;% freq(var)</code>.<br />
2018-12-31 12:03:35 +01:00
<code>top_freq</code> can be used to get the top/bottom <em>n</em> items of a frequency table, with counts as names.</p>
2018-12-29 22:24:19 +01:00
</div>
2019-05-29 19:56:17 +02:00
<pre class="usage"><span class='fu'>freq</span>(<span class='no'>x</span>, <span class='no'>...</span>, <span class='kw'>sort.count</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>nmax</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a></span>(<span class='st'>"max.print.freq"</span>),
2018-12-29 22:24:19 +01:00
<span class='kw'>na.rm</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>row.names</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>markdown</span> <span class='kw'>=</span> !<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/interactive'>interactive</a></span>(),
2019-07-04 15:26:07 +02:00
<span class='kw'>digits</span> <span class='kw'>=</span> <span class='fl'>2</span>, <span class='kw'>quote</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>header</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>title</span> <span class='kw'>=</span> <span class='kw'>NULL</span>,
2018-12-29 22:24:19 +01:00
<span class='kw'>na</span> <span class='kw'>=</span> <span class='st'>"&lt;NA&gt;"</span>, <span class='kw'>droplevels</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>sep</span> <span class='kw'>=</span> <span class='st'>" "</span>,
<span class='kw'>decimal.mark</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a></span>(<span class='st'>"OutDec"</span>), <span class='kw'>big.mark</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/ifelse'>ifelse</a></span>(<span class='no'>decimal.mark</span> <span class='kw'>!=</span>
<span class='st'>","</span>, <span class='st'>","</span>, <span class='st'>"."</span>))
2019-05-29 19:56:17 +02:00
<span class='fu'>frequency_tbl</span>(<span class='no'>x</span>, <span class='no'>...</span>, <span class='kw'>sort.count</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
<span class='kw'>nmax</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a></span>(<span class='st'>"max.print.freq"</span>), <span class='kw'>na.rm</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>row.names</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
2019-07-04 15:26:07 +02:00
<span class='kw'>markdown</span> <span class='kw'>=</span> !<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/interactive'>interactive</a></span>(), <span class='kw'>digits</span> <span class='kw'>=</span> <span class='fl'>2</span>, <span class='kw'>quote</span> <span class='kw'>=</span> <span class='kw'>NULL</span>,
2019-05-29 19:56:17 +02:00
<span class='kw'>header</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>, <span class='kw'>title</span> <span class='kw'>=</span> <span class='kw'>NULL</span>, <span class='kw'>na</span> <span class='kw'>=</span> <span class='st'>"&lt;NA&gt;"</span>, <span class='kw'>droplevels</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
<span class='kw'>sep</span> <span class='kw'>=</span> <span class='st'>" "</span>, <span class='kw'>decimal.mark</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a></span>(<span class='st'>"OutDec"</span>),
<span class='kw'>big.mark</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/ifelse'>ifelse</a></span>(<span class='no'>decimal.mark</span> <span class='kw'>!=</span> <span class='st'>","</span>, <span class='st'>","</span>, <span class='st'>"."</span>))
2018-12-29 22:24:19 +01:00
<span class='fu'>top_freq</span>(<span class='no'>f</span>, <span class='no'>n</span>)
<span class='fu'>header</span>(<span class='no'>f</span>, <span class='kw'>property</span> <span class='kw'>=</span> <span class='kw'>NULL</span>)
2019-05-29 19:56:17 +02:00
<span class='co'># S3 method for freq</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/print'>print</a></span>(<span class='no'>x</span>, <span class='kw'>nmax</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a></span>(<span class='st'>"max.print.freq"</span>, <span class='kw'>default</span> <span class='kw'>=</span> <span class='fl'>15</span>),
<span class='kw'>markdown</span> <span class='kw'>=</span> !<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/interactive'>interactive</a></span>(), <span class='kw'>header</span> <span class='kw'>=</span> <span class='fl'>TRUE</span>,
2018-12-29 22:24:19 +01:00
<span class='kw'>decimal.mark</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a></span>(<span class='st'>"OutDec"</span>), <span class='kw'>big.mark</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/ifelse'>ifelse</a></span>(<span class='no'>decimal.mark</span> <span class='kw'>!=</span>
<span class='st'>","</span>, <span class='st'>","</span>, <span class='st'>"."</span>), <span class='no'>...</span>)</pre>
<h2 class="hasAnchor" id="arguments"><a class="anchor" href="#arguments"></a>Arguments</h2>
<table class="ref-arguments">
<colgroup><col class="name" /><col class="desc" /></colgroup>
<tr>
<th>x</th>
<td><p>vector of any class or a <code><a href='https://www.rdocumentation.org/packages/base/topics/data.frame'>data.frame</a></code>, <code>tibble</code> (may contain a grouping variable) or <code><a href='https://www.rdocumentation.org/packages/base/topics/table'>table</a></code></p></td>
</tr>
<tr>
<th>...</th>
2019-01-28 11:20:32 +01:00
<td><p>up to nine different columns of <code>x</code> when <code>x</code> is a <code>data.frame</code> or <code>tibble</code>, to calculate frequencies from - see Examples. Also supports quasiquotion.</p></td>
2018-12-29 22:24:19 +01:00
</tr>
<tr>
<th>sort.count</th>
<td><p>sort on count, i.e. frequencies. This will be <code>TRUE</code> at default for everything except when using grouping variables.</p></td>
</tr>
<tr>
<th>nmax</th>
<td><p>number of row to print. The default, <code>15</code>, uses <code><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a>("max.print.freq")</code>. Use <code>nmax = 0</code>, <code>nmax = Inf</code>, <code>nmax = NULL</code> or <code>nmax = NA</code> to print all rows.</p></td>
</tr>
<tr>
<th>na.rm</th>
<td><p>a logical value indicating whether <code>NA</code> values should be removed from the frequency table. The header (if set) will always print the amount of <code>NA</code>s.</p></td>
</tr>
<tr>
<th>row.names</th>
<td><p>a logical value indicating whether row indices should be printed as <code>1:nrow(x)</code></p></td>
</tr>
<tr>
<th>markdown</th>
<td><p>a logical value indicating whether the frequency table should be printed in markdown format. This will print all rows (except when <code>nmax</code> is defined) and is default behaviour in non-interactive R sessions (like when knitting RMarkdown files).</p></td>
</tr>
<tr>
<th>digits</th>
<td><p>how many significant digits are to be used for numeric values in the header (not for the items themselves, that depends on <code><a href='https://www.rdocumentation.org/packages/base/topics/options'>getOption</a>("digits")</code>)</p></td>
</tr>
<tr>
<th>quote</th>
2019-07-04 15:26:07 +02:00
<td><p>a logical value indicating whether or not strings should be printed with surrounding quotes. Default is to print them only around characters that are actually numeric values.</p></td>
2018-12-29 22:24:19 +01:00
</tr>
<tr>
<th>header</th>
<td><p>a logical value indicating whether an informative header should be printed</p></td>
</tr>
<tr>
<th>title</th>
<td><p>text to show above frequency table, at default to tries to coerce from the variables passed to <code>x</code></p></td>
</tr>
<tr>
<th>na</th>
2018-12-31 12:03:35 +01:00
<td><p>a character string that should be used to show empty (<code>NA</code>) values (only useful when <code>na.rm = FALSE</code>)</p></td>
2018-12-29 22:24:19 +01:00
</tr>
<tr>
<th>droplevels</th>
<td><p>a logical value indicating whether in factors empty levels should be dropped</p></td>
</tr>
<tr>
<th>sep</th>
<td><p>a character string to separate the terms when selecting multiple columns</p></td>
</tr>
<tr>
<th>decimal.mark</th>
<td><p><!-- % -->
used for prettying (longish) numerical and complex sequences.
Passed to <code><a href='https://www.rdocumentation.org/packages/base/topics/formatc'>prettyNum</a></code>: that help page explains the details.</p></td>
</tr>
<tr>
<th>big.mark</th>
<td><p><!-- % -->
used for prettying (longish) numerical and complex sequences.
Passed to <code><a href='https://www.rdocumentation.org/packages/base/topics/formatc'>prettyNum</a></code>: that help page explains the details.</p></td>
</tr>
<tr>
<th>f</th>
<td><p>a frequency table</p></td>
</tr>
<tr>
<th>n</th>
<td><p>number of top <em>n</em> items to return, use -n for the bottom <em>n</em> items. It will include more than <code>n</code> rows if there are ties.</p></td>
</tr>
<tr>
<th>property</th>
<td><p>property in header to return this value directly</p></td>
</tr>
</table>
<h2 class="hasAnchor" id="value"><a class="anchor" href="#value"></a>Value</h2>
2019-05-29 19:56:17 +02:00
<p>A <code>data.frame</code> (with an additional class <code>"freq"</code>) with five columns: <code>item</code>, <code>count</code>, <code>percent</code>, <code>cum_count</code> and <code>cum_percent</code>.</p>
2018-12-29 22:24:19 +01:00
<h2 class="hasAnchor" id="details"><a class="anchor" href="#details"></a>Details</h2>
<p>Frequency tables (or frequency distributions) are summaries of the distribution of values in a sample. With the `freq` function, you can create univariate frequency tables. Multiple variables will be pasted into one variable, so it forces a univariate distribution. This package also has a vignette available to explain the use of this function further, run <code><a href='https://www.rdocumentation.org/packages/utils/topics/browseVignettes'>browseVignettes("AMR")</a></code> to read it.</p>
<p>For numeric values of any class, these additional values will all be calculated with <code>na.rm = TRUE</code> and shown into the header:</p><ul>
<li><p>Mean, using <code><a href='https://www.rdocumentation.org/packages/base/topics/mean'>mean</a></code></p></li>
<li><p>Standard Deviation, using <code><a href='https://www.rdocumentation.org/packages/stats/topics/sd'>sd</a></code></p></li>
<li><p>Coefficient of Variation (CV), the standard deviation divided by the mean</p></li>
<li><p>Mean Absolute Deviation (MAD), using <code><a href='https://www.rdocumentation.org/packages/stats/topics/mad'>mad</a></code></p></li>
<li><p>Tukey Five-Number Summaries (minimum, Q1, median, Q3, maximum), using <code><a href='https://www.rdocumentation.org/packages/stats/topics/fivenum'>fivenum</a></code></p></li>
<li><p>Interquartile Range (IQR) calculated as <code>Q3 - Q1</code> using the Tukey Five-Number Summaries, i.e. <strong>not</strong> using the <code><a href='https://www.rdocumentation.org/packages/stats/topics/quantile'>quantile</a></code> function</p></li>
<li><p>Coefficient of Quartile Variation (CQV, sometimes called coefficient of dispersion), calculated as <code>(Q3 - Q1) / (Q3 + Q1)</code> using the Tukey Five-Number Summaries</p></li>
<li><p>Outliers (total count and unique count), using <code><a href='https://www.rdocumentation.org/packages/grDevices/topics/boxplot.stats'>boxplot.stats</a></code></p></li>
</ul>
<p>For dates and times of any class, these additional values will be calculated with <code>na.rm = TRUE</code> and shown into the header:</p><ul>
<li><p>Oldest, using <code><a href='https://www.rdocumentation.org/packages/base/topics/Extremes'>min</a></code></p></li>
<li><p>Newest, using <code><a href='https://www.rdocumentation.org/packages/base/topics/Extremes'>max</a></code>, with difference between newest and oldest</p></li>
<li><p>Median, using <code><a href='https://www.rdocumentation.org/packages/stats/topics/median'>median</a></code>, with percentage since oldest</p></li>
</ul>
<p>In factors, all factor levels that are not existing in the input data will be dropped.</p>
2019-02-08 16:06:54 +01:00
<p>The function <code>top_freq</code> uses <code><a href='https://dplyr.tidyverse.org/reference/top_n.html'>top_n</a></code> internally and will include more than <code>n</code> rows if there are ties.</p>
2018-12-29 22:24:19 +01:00
2019-01-02 23:24:07 +01:00
<h2 class="hasAnchor" id="read-more-on-our-website-"><a class="anchor" href="#read-more-on-our-website-"></a>Read more on our website!</h2>
<p>On our website <a href='https://msberends.gitlab.io/AMR'>https://msberends.gitlab.io/AMR</a> you can find <a href='https://msberends.gitlab.io/AMR/articles/AMR.html'>a tutorial</a> about how to conduct AMR analysis, the <a href='https://msberends.gitlab.io/AMR/reference'>complete documentation of all functions</a> (which reads a lot easier than here in R) and <a href='https://msberends.gitlab.io/AMR/articles/WHONET.html'>an example analysis using WHONET data</a>.</p>
2019-01-02 23:24:07 +01:00
2018-12-29 22:24:19 +01:00
<h2 class="hasAnchor" id="examples"><a class="anchor" href="#examples"></a>Examples</h2>
2019-01-02 23:24:07 +01:00
<pre class="examples"><span class='co'># NOT RUN {</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/library'>library</a></span>(<span class='no'>dplyr</span>)
2018-12-29 22:24:19 +01:00
<span class='co'># this all gives the same result:</span>
2019-01-02 23:24:07 +01:00
<span class='fu'>freq</span>(<span class='no'>septic_patients</span>$<span class='no'>hospital_id</span>)
<span class='fu'>freq</span>(<span class='no'>septic_patients</span>[, <span class='st'>"hospital_id"</span>])
<span class='no'>septic_patients</span>$<span class='no'>hospital_id</span> <span class='kw'>%&gt;%</span> <span class='fu'>freq</span>()
<span class='no'>septic_patients</span>[, <span class='st'>"hospital_id"</span>] <span class='kw'>%&gt;%</span> <span class='fu'>freq</span>()
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span> <span class='fu'>freq</span>(<span class='st'>"hospital_id"</span>)
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span> <span class='fu'>freq</span>(<span class='no'>hospital_id</span>) <span class='co'>#&lt;- easiest to remember (tidyverse)</span>
2018-12-29 22:24:19 +01:00
<span class='co'># you could also use `select` or `pull` to get your variables</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
2019-02-08 16:06:54 +01:00
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/filter.html'>filter</a></span>(<span class='no'>hospital_id</span> <span class='kw'>==</span> <span class='st'>"A"</span>) <span class='kw'>%&gt;%</span>
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/select.html'>select</a></span>(<span class='no'>mo</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'>freq</span>()
2018-12-29 22:24:19 +01:00
<span class='co'># multiple selected variables will be pasted together</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='no'>left_join_microorganisms</span> <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'>freq</span>(<span class='no'>genus</span>, <span class='no'>species</span>)
2019-01-28 11:20:32 +01:00
<span class='co'># functions as quasiquotation are also supported</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='fu'><a href='mo_property.html'>mo_genus</a></span>(<span class='no'>mo</span>), <span class='fu'><a href='mo_property.html'>mo_species</a></span>(<span class='no'>mo</span>))
2018-12-29 22:24:19 +01:00
<span class='co'># group a variable and analyse another</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
2019-02-08 16:06:54 +01:00
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/group_by.html'>group_by</a></span>(<span class='no'>hospital_id</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'>freq</span>(<span class='no'>gender</span>)
2018-12-29 22:24:19 +01:00
<span class='co'># get top 10 bugs of hospital A as a vector</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
2019-02-08 16:06:54 +01:00
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/filter.html'>filter</a></span>(<span class='no'>hospital_id</span> <span class='kw'>==</span> <span class='st'>"A"</span>) <span class='kw'>%&gt;%</span>
2018-12-29 22:24:19 +01:00
<span class='fu'>freq</span>(<span class='no'>mo</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'>top_freq</span>(<span class='fl'>10</span>)
2018-12-29 22:24:19 +01:00
<span class='co'># save frequency table to an object</span>
<span class='no'>years</span> <span class='kw'>&lt;-</span> <span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
2019-02-08 16:06:54 +01:00
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/mutate.html'>mutate</a></span>(<span class='kw'>year</span> <span class='kw'>=</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/format'>format</a></span>(<span class='no'>date</span>, <span class='st'>"%Y"</span>)) <span class='kw'>%&gt;%</span>
2018-12-29 22:24:19 +01:00
<span class='fu'>freq</span>(<span class='no'>year</span>)
<span class='co'># show only the top 5</span>
2019-01-02 23:24:07 +01:00
<span class='no'>years</span> <span class='kw'>%&gt;%</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/print'>print</a></span>(<span class='kw'>nmax</span> <span class='kw'>=</span> <span class='fl'>5</span>)
2018-12-29 22:24:19 +01:00
<span class='co'># save to an object with formatted percentages</span>
<span class='no'>years</span> <span class='kw'>&lt;-</span> <span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/format'>format</a></span>(<span class='no'>years</span>)
<span class='co'># print a histogram of numeric values</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'><a href='https://www.rdocumentation.org/packages/graphics/topics/hist'>hist</a></span>()
2019-05-29 19:56:17 +02:00
<span class='co'># or a boxplot of numeric values</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/graphics/topics/boxplot'>boxplot</a></span>()
<span class='co'># or even a boxplot per group</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/group_by.html'>group_by</a></span>(<span class='no'>hospital_id</span>) <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/graphics/topics/boxplot'>boxplot</a></span>()
2018-12-29 22:24:19 +01:00
<span class='co'># or print all points to a regular plot</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'><a href='https://www.rdocumentation.org/packages/graphics/topics/plot'>plot</a></span>()
2018-12-29 22:24:19 +01:00
<span class='co'># transform to a data.frame or tibble</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/as.data.frame'>as.data.frame</a></span>()
2018-12-29 22:24:19 +01:00
<span class='co'># or transform (back) to a vector</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/vector'>as.vector</a></span>()
2018-12-29 22:24:19 +01:00
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/identical'>identical</a></span>(<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/vector'>as.vector</a></span>() <span class='kw'>%&gt;%</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/sort'>sort</a></span>(),
2019-01-02 23:24:07 +01:00
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/sort'>sort</a></span>(<span class='no'>septic_patients</span>$<span class='no'>age</span>)) <span class='co'># TRUE</span>
2018-12-29 22:24:19 +01:00
<span class='co'># it also supports `table` objects</span>
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/table'>table</a></span>(<span class='no'>septic_patients</span>$<span class='no'>gender</span>,
<span class='no'>septic_patients</span>$<span class='no'>age</span>) <span class='kw'>%&gt;%</span>
2019-01-02 23:24:07 +01:00
<span class='fu'>freq</span>(<span class='kw'>sep</span> <span class='kw'>=</span> <span class='st'>" **sep** "</span>)
2018-12-29 22:24:19 +01:00
<span class='co'># only get selected columns</span>
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>hospital_id</span>) <span class='kw'>%&gt;%</span>
2019-02-08 16:06:54 +01:00
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/select.html'>select</a></span>(<span class='no'>item</span>, <span class='no'>percent</span>)
2019-01-02 23:24:07 +01:00
2018-12-29 22:24:19 +01:00
<span class='no'>septic_patients</span> <span class='kw'>%&gt;%</span>
<span class='fu'>freq</span>(<span class='no'>hospital_id</span>) <span class='kw'>%&gt;%</span>
2019-02-08 16:06:54 +01:00
<span class='fu'><a href='https://dplyr.tidyverse.org/reference/select.html'>select</a></span>(-<span class='no'>count</span>, -<span class='no'>cum_count</span>)
2019-01-02 23:24:07 +01:00
2018-12-29 22:24:19 +01:00
<span class='co'># check differences between frequency tables</span>
2019-05-10 16:44:59 +02:00
<span class='fu'><a href='https://www.rdocumentation.org/packages/base/topics/diff'>diff</a></span>(<span class='fu'>freq</span>(<span class='no'>septic_patients</span>$<span class='no'>TMP</span>),
<span class='fu'>freq</span>(<span class='no'>septic_patients</span>$<span class='no'>SXT</span>))
2019-01-02 23:24:07 +01:00
<span class='co'># }</span></pre>
2018-12-29 22:24:19 +01:00
</div>
<div class="col-md-3 hidden-xs hidden-sm" id="sidebar">
<h2>Contents</h2>
<ul class="nav nav-pills nav-stacked">
<li><a href="#arguments">Arguments</a></li>
<li><a href="#value">Value</a></li>
<li><a href="#details">Details</a></li>
2019-01-02 23:24:07 +01:00
<li><a href="#read-more-on-our-website-">Read more on our website!</a></li>
2018-12-29 22:24:19 +01:00
<li><a href="#examples">Examples</a></li>
</ul>
</div>
</div>
<footer>
<div class="copyright">
<p>Developed by <a href='https://www.rug.nl/staff/m.s.berends/'>Matthijs S. Berends</a>, <a href='https://www.rug.nl/staff/c.f.luz/'>Christian F. Luz</a>, <a href='https://www.rug.nl/staff/c.glasner/'>Corinna Glasner</a>, <a href='https://www.rug.nl/staff/a.w.friedrich/'>Alex W. Friedrich</a>, <a href='https://www.rug.nl/staff/b.sinha/'>Bhanu N. M. Sinha</a>.</p>
</div>
<div class="pkgdown">
<p>Site built with <a href="https://pkgdown.r-lib.org/">pkgdown</a> 1.3.0.</p>
</div>
</footer>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/docsearch.js/2.6.1/docsearch.min.js" integrity="sha256-GKvGqXDznoRYHCwKXGnuchvKSwmx9SRMrZOTh2g4Sb0=" crossorigin="anonymous"></script>
<script>
docsearch({
apiKey: 'f737050abfd4d726c63938e18f8c496e',
indexName: 'amr',
inputSelector: 'input#search-input.form-control',
transformData: function(hits) {
return hits.map(function (hit) {
hit.url = updateHitURL(hit);
return hit;
});
}
});
</script>
</body>
</html>