added vignette of freq

This commit is contained in:
dr. M.S. (Matthijs) Berends committed 2018-05-09 11:44:46 +02:00
1 parent 25b3346d9a
commit f05e7178cb
13 files changed
+726 -82

No files matched your search

+1
View File
@@ -4,3 +4,4 @@
.Ruserdata
AMR.Rproj
tests/testthat/Rplots.pdf
inst/doc
+6 -3
View File
@@ -1,6 +1,6 @@
Package: AMR
Version: 0.2.0
Date: 2018-05-02
Version: 0.2.0.9000
Date: 2018-05-09
Title: Antimicrobial Resistance Analysis
Authors@R: c(
person(
@@ -37,7 +37,10 @@ Imports:
tibble
Suggests:
testthat (>= 1.0.2),
covr (>= 3.0.1)
covr (>= 3.0.1),
knitr,
rmarkdown
VignetteBuilder: knitr
URL: https://github.com/msberends/AMR
BugReports: https://github.com/msberends/AMR/issues
License: GPL-2 | file LICENSE
+10 -1
View File
@@ -1,4 +1,13 @@
# 0.2.0
# 0.2.9000 (development version)
#### New
* Vignettes about frequency tables: [vignettes/freq.html](vignettes/freq.html)
* Possibility to globally set the default for the amount of items to print in frequency tables (`freq` function), with `options(max.print.freq = n)`
#### Changed
* Renamed `toConsole` parameter of `freq` to `as.data.frame`
* Small translational improvements to the `septic_patients` dataset
# 0.2.0 (latest stable version)
#### New
* Full support for Windows, Linux and macOS
* Full support for old R versions, only R-3.0.0 (April 2013) or later is needed (needed packages may have other dependencies)
+73 -62
View File
@@ -21,10 +21,10 @@
#' Create a frequency table of a vector of data, a single column or a maximum of 9 columns of a data frame. Supports markdown for reports.
#' @param x data
#' @param sort.count Sort on count. Use \code{FALSE} to sort alphabetically on item.
#' @param nmax number of row to print. Use \code{nmax = 0} or \code{nmax = NA} to print all rows.
#' @param na.rm a logical value indicating whether NA values should be removed from the frequency table. The header will always print the amount of\code{NA}s.
#' @param nmax number of row to print. The default, \code{15}, uses \code{\link[base]{getOption}("max.print.freq")}. Use \code{nmax = 0} or \code{nmax = NA} to print all rows.
#' @param na.rm a logical value indicating whether NA values should be removed from the frequency table. The header will always print the amount of \code{NA}s.
#' @param markdown print table in markdown format (this forces \code{nmax = NA})
#' @param toConsole Print table to the console. Use \code{FALSE} to assign the table to an object.
#' @param as.data.frame return frequency table without header as a \code{data.frame} (e.g. to assign the table to an object)
#' @param digits how many significant digits are to be used for numeric values (not for the items themselves, that depends on \code{\link{getOption}("digits")})
#' @param sep a character string to separate the terms when selecting multiple columns
#' @details For numeric values, the next values will be calculated and shown into the header:
@@ -32,7 +32,7 @@
#' \item{Mean, using \code{\link[base]{mean}}}
#' \item{Standard deviation, using \code{\link[stats]{sd}}}
#' \item{Five numbers of Tukey (min, Q1, median, Q3, max), using \code{\link[stats]{fivenum}}}
#' \item{Outliers (count and list), using \code{\link{boxplot.stats}}}
#' \item{Outliers (total count and unique count), using \code{\link{boxplot.stats}}}
#' \item{Coefficient of variation (CV), the standard deviation divided by the mean}
#' \item{Coefficient of quartile variation (CQV, sometimes called coefficient of dispersion), calculated as \code{(Q3 - Q1) / (Q3 + Q1)} using \code{\link{quantile}} with \code{type = 6} as quantile algorithm to comply with SPSS standards}
#' }
@@ -63,13 +63,13 @@
#' years <- septic_patients %>%
#' mutate(year = format(date, "%Y")) %>%
#' select(year) %>%
#' freq(toConsole = FALSE)
#' freq(as.data.frame = TRUE)
freq <- function(x,
sort.count = TRUE,
nmax = 15,
nmax = getOption("max.print.freq"),
na.rm = TRUE,
markdown = FALSE,
toConsole = TRUE,
as.data.frame = FALSE,
digits = 2,
sep = " ") {
@@ -156,8 +156,8 @@ freq <- function(x,
stop('A maximum of 9 columns can be analysed at the same time.', call. = FALSE)
}
}
if (markdown == TRUE & toConsole == FALSE) {
warning('`toConsole = FALSE` will be ignored when `markdown = TRUE`.')
if (markdown == TRUE & as.data.frame == TRUE) {
warning('`as.data.frame = TRUE` will be ignored when `markdown = TRUE`.')
}
if (mult.columns > 1) {
@@ -232,7 +232,7 @@ freq <- function(x,
x <- x %>% format(formatdates)
}
if (toConsole == TRUE) {
if (as.data.frame == FALSE) {
cat(header)
}
@@ -244,22 +244,30 @@ freq <- function(x,
warning('All observations are unique.', call. = FALSE)
}
if (nmax == 0 | is.na(nmax)) {
nmax.set <- !missing(nmax)
if (is.null(nmax) & is.null(base::getOption("max.print.freq", default = NULL))) {
# default for max print setting
nmax <- 15
}
if (nmax == 0 | is.na(nmax) | is.null(nmax)) {
nmax <- length(x)
}
nmax.1 <- min(length(x), nmax + 1)
# create table with counts and percentages
column_names <- c('Item', 'Count', 'Percent', 'Cum. Count', 'Cum. Percent', '(Factor Level)')
column_names_df <- c('item', 'count', 'percent', 'cum_count', 'cum_percent', 'factor_level')
if (any(class(x) == 'factor')) {
df <- tibble::tibble(Item = x,
Fctlvl = x %>% as.integer()) %>%
group_by(Item, Fctlvl)
column_names <- c('Item', 'Count', 'Percent', 'Cum. Count', 'Cum. Percent', '(Factor Level)')
column_align <- c('l', 'r', 'r', 'r', 'r', 'r')
} else {
df <- tibble::tibble(Item = x) %>%
group_by(Item)
column_names <- c('Item', 'Count', 'Percent', 'Cum. Count', 'Cum. Percent')
column_names <- column_names[1:5] # strip factor lvl
column_names_df <- column_names_df[1:5] # strip factor lvl
column_align <- c(x_align, 'r', 'r', 'r', 'r')
}
df <- df %>%
@@ -276,10 +284,10 @@ freq <- function(x,
# sort according to setting
if (sort.count == TRUE) {
df <- df %>% arrange(desc(Count))
df <- df %>% arrange(desc(Count), Item)
} else {
if (any(class(x) == 'factor')) {
df <- df %>% arrange(Fctlvl)
df <- df %>% arrange(Fctlvl, Item)
} else {
df <- df %>% arrange(Item)
}
@@ -295,65 +303,68 @@ freq <- function(x,
df <- df %>% select(Item, Count, Percent, Cum, CumTot, Fctlvl)
}
if (as.data.frame == TRUE) {
# assign to object
df[, 3] <- df[, 2] / sum(df[, 2], na.rm = TRUE)
df[, 4] <- cumsum(df[, 2])
df[, 5] <- df[, 4] / sum(df[, 2], na.rm = TRUE)
colnames(df) <- column_names_df
return(as.data.frame(df, stringsAsFactors = FALSE))
}
if (markdown == TRUE) {
tblformat <- 'markdown'
} else {
tblformat <- 'pandoc'
}
if (toConsole == FALSE) {
# assign to object
df[, 3] <- df[, 2] / sum(df[, 2], na.rm = TRUE)
df[, 4] <- cumsum(df[, 2])
df[, 5] <- df[, 4] / sum(df[, 2], na.rm = TRUE)
return(df)
# save old NA setting for kable
opt.old <- options()$knitr.kable.NA
options(knitr.kable.NA = "<NA>")
} else {
Count.rest <- sum(df[nmax.1:nrow(df), 'Count'], na.rm = TRUE)
if (any(class(x) %in% c('double', 'integer', 'numeric', 'raw', 'single'))) {
df <- df %>% mutate(Item = format(Item))
}
df <- df %>% mutate(Count = format(Count))
# save old NA setting for kable
opt.old <- options()$knitr.kable.NA
options(knitr.kable.NA = "<NA>")
Count.rest <- sum(df[nmax.1:nrow(df), 'Count'], na.rm = TRUE)
if (any(class(x) %in% c('double', 'integer', 'numeric', 'raw', 'single'))) {
df <- df %>% mutate(Item = format(Item))
}
df <- df %>% mutate(Count = format(Count))
if (nrow(df) > nmax.1 & markdown == FALSE) {
df2 <- df[1:nmax,]
print(
knitr::kable(df2,
format = tblformat,
col.names = column_names,
align = column_align,
padding = 1)
)
cat('... and ',
format(nrow(df) - nmax),
' more ',
paste0('(n = ',
format(Count.rest),
'; ',
(Count.rest / length(x)) %>% percent(force_zero = TRUE),
')'),
'. Use `nmax` to show more rows.\n', sep = '')
} else {
print(
knitr::kable(df,
format = tblformat,
col.names = column_names,
align = column_align,
padding = 1)
)
if (nrow(df) > nmax.1 & markdown == FALSE) {
df2 <- df[1:nmax,]
print(
knitr::kable(df2,
format = tblformat,
col.names = column_names,
align = column_align,
padding = 1)
)
cat('... and ',
format(nrow(df) - nmax),
' more ',
paste0('(n = ',
format(Count.rest),
'; ',
(Count.rest / length(x)) %>% percent(force_zero = TRUE),
')'),
'.', sep = '')
if (nmax.set == FALSE) {
cat(' Use `nmax` to show more or less rows.')
}
cat('\n')
# reset old kable setting
options(knitr.kable.NA = opt.old)
return(invisible())
} else {
print(
knitr::kable(df,
format = tblformat,
col.names = column_names,
align = column_align,
padding = 1)
)
}
cat('\n')
# reset old kable setting
options(knitr.kable.NA = opt.old)
return(invisible())
}
#' @rdname freq
+1 -1
View File
@@ -3,7 +3,7 @@
[![logo_rug](man/figures/logo_rug.png)](https://www.rug.nl)[![logo_umcg](man/figures/logo_umcg.png)](https://www.umcg.nl)
This R package was created for academic research by PhD students of the Faculty of Medical Sciences of the [University of Groningen)](https://www.rug.nl) and the Medical Microbiology & Infection Prevention (MMBI) department of the [University Medical Center Groningen (UMCG)](https://www.umcg.nl). See [Authors](#authors).
This R package was created for academic research by PhD students of the Faculty of Medical Sciences of the [University of Groningen](https://www.rug.nl) and the Medical Microbiology & Infection Prevention (MMBI) department of the [University Medical Center Groningen (UMCG)](https://www.umcg.nl). See [Authors](#authors).
## Why this package?
This R package contains functions to make **microbiological, epidemiological data analysis easier**. It allows the use of some new classes to work with MIC values and antimicrobial interpretations (i.e. values S, I and R).
Binary file not shown.
+11 -9
View File
@@ -5,24 +5,26 @@
\alias{frequency_tbl}
\title{Frequency table}
\usage{
freq(x, sort.count = TRUE, nmax = 15, na.rm = TRUE, markdown = FALSE,
toConsole = TRUE, digits = 2, sep = " ")
freq(x, sort.count = TRUE, nmax = getOption("max.print.freq"),
na.rm = TRUE, markdown = FALSE, as.data.frame = FALSE, digits = 2,
sep = " ")
frequency_tbl(x, sort.count = TRUE, nmax = 15, na.rm = TRUE,
markdown = FALSE, toConsole = TRUE, digits = 2, sep = " ")
frequency_tbl(x, sort.count = TRUE, nmax = getOption("max.print.freq"),
na.rm = TRUE, markdown = FALSE, as.data.frame = FALSE, digits = 2,
sep = " ")
}
\arguments{
\item{x}{data}
\item{sort.count}{Sort on count. Use \code{FALSE} to sort alphabetically on item.}
\item{nmax}{number of row to print. Use \code{nmax = 0} or \code{nmax = NA} to print all rows.}
\item{nmax}{number of row to print. The default, \code{15}, uses \code{\link[base]{getOption}("max.print.freq")}. Use \code{nmax = 0} or \code{nmax = NA} to print all rows.}
\item{na.rm}{a logical value indicating whether NA values should be removed from the frequency table. The header will always print the amount of\code{NA}s.}
\item{na.rm}{a logical value indicating whether NA values should be removed from the frequency table. The header will always print the amount of \code{NA}s.}
\item{markdown}{print table in markdown format (this forces \code{nmax = NA})}
\item{toConsole}{Print table to the console. Use \code{FALSE} to assign the table to an object.}
\item{as.data.frame}{return frequency table without header as a \code{data.frame} (e.g. to assign the table to an object)}
\item{digits}{how many significant digits are to be used for numeric values (not for the items themselves, that depends on \code{\link{getOption}("digits")})}
@@ -37,7 +39,7 @@ For numeric values, the next values will be calculated and shown into the header
\item{Mean, using \code{\link[base]{mean}}}
\item{Standard deviation, using \code{\link[stats]{sd}}}
\item{Five numbers of Tukey (min, Q1, median, Q3, max), using \code{\link[stats]{fivenum}}}
\item{Outliers (count and list), using \code{\link{boxplot.stats}}}
\item{Outliers (total count and unique count), using \code{\link{boxplot.stats}}}
\item{Coefficient of variation (CV), the standard deviation divided by the mean}
\item{Coefficient of quartile variation (CQV, sometimes called coefficient of dispersion), calculated as \code{(Q3 - Q1) / (Q3 + Q1)} using \code{\link{quantile}} with \code{type = 6} as quantile algorithm to comply with SPSS standards}
}
@@ -63,7 +65,7 @@ septic_patients \%>\%
years <- septic_patients \%>\%
mutate(year = format(date, "\%Y")) \%>\%
select(year) \%>\%
freq(toConsole = FALSE)
freq(as.data.frame = TRUE)
}
\keyword{freq}
\keyword{frequency}
+1 -1
View File
@@ -1,7 +1,7 @@
context("eucast.R")
test_that("EUCAST rules work", {
a <- EUCAST_rules(septic_patients)
a <- suppressWarnings(EUCAST_rules(septic_patients))
a <- data.frame(bactid = c("KLEPNE", # Klebsiella pneumoniae
"PSEAER", # Pseudomonas aeruginosa
+4 -4
View File
@@ -1,12 +1,12 @@
context("freq.R")
test_that("frequency table works", {
expect_equal(nrow(freq(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), toConsole = FALSE)), 5)
expect_equal(nrow(frequency_tbl(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), toConsole = FALSE)), 5)
expect_equal(nrow(freq(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), as.data.frame = TRUE)), 5)
expect_equal(nrow(frequency_tbl(c(1, 1, 2, 2, 3, 3, 4, 4, 5, 5), as.data.frame = TRUE)), 5)
# date column of septic_patients should contain 1662 unique dates
expect_equal(nrow(freq(septic_patients$date, toConsole = FALSE)), 1662)
expect_equal(nrow(freq(septic_patients$date, toConsole = FALSE)),
expect_equal(nrow(freq(septic_patients$date, as.data.frame = TRUE)), 1662)
expect_equal(nrow(freq(septic_patients$date, as.data.frame = TRUE)),
length(unique(septic_patients$date)))
expect_output(freq(septic_patients$age))
+1 -1
View File
@@ -13,7 +13,7 @@ test_that("MDRO works", {
expect_equal(outcome %>% class(), c('ordered', 'factor'))
# septic_patients should have these finding using Dutch guidelines
expect_equal(outcome %>% freq(toConsole = FALSE) %>% pull(Count), c(3, 21))
expect_equal(outcome %>% freq(as.data.frame = TRUE) %>% pull(count), c(3, 21))
expect_equal(BRMO(septic_patients, info = FALSE), MDRO(septic_patients, "nl", info = FALSE))
+91
View File
@@ -0,0 +1,91 @@
## ----setup, include = FALSE, results = 'markup'--------------------------
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#"
)
library(dplyr)
library(AMR)
## ---- echo = TRUE, results = 'hide'--------------------------------------
# # just using base R
freq(septic_patients$sex)
# # using base R to select the variable and pass it on with a pipe
septic_patients$sex %>% freq()
# # do it all with pipes, using the `select` function of the dplyr package
septic_patients %>%
select(sex) %>%
freq()
## ---- echo = TRUE--------------------------------------------------------
freq(septic_patients$sex)
## ---- echo = TRUE, results = 'hide'--------------------------------------
my_patients <- septic_patients %>%
left_join_microorganisms()
## ---- echo = TRUE--------------------------------------------------------
colnames(microorganisms)
## ---- echo = TRUE--------------------------------------------------------
dim(septic_patients)
dim(my_patients)
## ---- echo = TRUE--------------------------------------------------------
my_patients %>%
select(genus, species) %>%
freq()
## ---- echo = TRUE--------------------------------------------------------
# # get age distribution of unique patients
septic_patients %>%
distinct(patient_id, .keep_all = TRUE) %>%
select(age) %>%
freq(nmax = 5)
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(hospital_id) %>%
freq()
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(hospital_id) %>%
freq(sort.count = TRUE)
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(amox) %>%
freq()
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(date) %>%
freq(nmax = 5)
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(amox) %>%
freq(na.rm = FALSE)
## ---- echo = TRUE--------------------------------------------------------
septic_patients %>%
select(hospital_id) %>%
freq(markdown = TRUE)
## ---- echo = TRUE--------------------------------------------------------
my_df <- septic_patients %>%
select(hospital_id) %>%
freq(as.data.frame = TRUE)
my_df
class(my_df)
## ---- echo = FALSE-------------------------------------------------------
# this will print "2018" in 2018, and "2018-yyyy" after 2018.
yrs <- c(2018:format(Sys.Date(), "%Y"))
yrs <- c(min(yrs), max(yrs))
yrs <- paste(unique(yrs), collapse = "-")
+183
View File
@@ -0,0 +1,183 @@
---
title: "Creating Frequency Tables"
author: "Matthijs S. Berends"
output:
rmarkdown::html_vignette:
toc: true
vignette: >
%\VignetteIndexEntry{Vignette Title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r setup, include = FALSE, results = 'markup'}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#"
)
library(dplyr)
library(AMR)
```
## Introduction
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. We take the `septic_patients` dataset (included in this AMR package) as example.
## Frequencies of one variable
To only show and quickly review the content of one variable, you can just select this variable in various ways. Let's say we want to get the frequencies of the `sex` variable of the `septic_patients` dataset:
```{r, echo = TRUE, results = 'hide'}
# # just using base R
freq(septic_patients$sex)
# # using base R to select the variable and pass it on with a pipe
septic_patients$sex %>% freq()
# # do it all with pipes, using the `select` function of the dplyr package
septic_patients %>%
select(sex) %>%
freq()
```
This will all lead to the following table:
```{r, echo = TRUE}
freq(septic_patients$sex)
```
This immediately shows the class of the variable, its length and availability (i.e. the amount of `NA`), the amount of unique values and (most importantly) that among septic patients men are more prevalent than women.
## Frequencies of more than one variable
Multiple variables will be pasted into one variable to review individual cases, keeping a univariate frequency table.
For illustration, we could add some more variables to the `septic_patients` dataset to learn about bacterial properties:
```{r, echo = TRUE, results = 'hide'}
my_patients <- septic_patients %>%
left_join_microorganisms()
```
Now all variables of the `microorganisms` dataset have been joined to the `septic_patients` dataset. The `microorganisms` dataset consists of the following variables:
```{r, echo = TRUE}
colnames(microorganisms)
```
If we compare the dimensions between the old and new dataset, we can see that these `r ncol(my_patients) - ncol(septic_patients)` variables were added:
```{r, echo = TRUE}
dim(septic_patients)
dim(my_patients)
```
So now the `genus` and `species` variables are available. A frequency table of these combined variables can be created like this:
```{r, echo = TRUE}
my_patients %>%
select(genus, species) %>%
freq()
```
## Frequencies of numeric values
Frequency tables can be created of any input.
In case of numeric values (like integers, doubles, etc.) additional descriptive statistics will be calculated and shown into the header:
```{r, echo = TRUE}
# # get age distribution of unique patients
septic_patients %>%
distinct(patient_id, .keep_all = TRUE) %>%
select(age) %>%
freq(nmax = 5)
```
So the following properties are determined, where `NA` values are always ignored:
* **Mean**
* **Standard deviation**
* **Coefficient of variation** (CV), the standard deviation divided by the mean
* **Five numbers of Tukey** (min, Q1, median, Q3, max)
* **Coefficient of quartile variation** (CQV, sometimes called coefficient of dispersion), calculated as (Q3 - Q1) / (Q3 + Q1) using quantile with `type = 6` as quantile algorithm to comply with SPSS standards
* **Outliers** (total count and unique count)
So for example, the above frequency table quickly shows the median age of patients being `r my_patients %>% distinct(patient_id, .keep_all = TRUE) %>% pull(age) %>% median(na.rm = TRUE)`.
## Frequencies of factors
Frequencies of factors will be sorted on factor level instead of item count by default. This can be changed with the `sort.count` parameter. Frequency tables of factors always show the factor level as an additional last column.
`sort.count` is `TRUE` by default, except for factors. Compare this default behaviour:
```{r, echo = TRUE}
septic_patients %>%
select(hospital_id) %>%
freq()
```
To this, where items are now sorted on item count:
```{r, echo = TRUE}
septic_patients %>%
select(hospital_id) %>%
freq(sort.count = TRUE)
```
All classes will be printed into the header. Variables with the new `rsi` class of this AMR package are actually ordered factors and have three classes (look at `Class` in the header):
```{r, echo = TRUE}
septic_patients %>%
select(amox) %>%
freq()
```
## Frequencies of dates
Frequencies of dates will show the oldest and newest date in the data, and the amount of days between them:
```{r, echo = TRUE}
septic_patients %>%
select(date) %>%
freq(nmax = 5)
```
## Additional parameters
### Parameter `na.rm`
With the `na.rm` parameter (defaults to `TRUE`, but they will always be shown into the header), you can include `NA` values in the frequency table:
```{r, echo = TRUE}
septic_patients %>%
select(amox) %>%
freq(na.rm = FALSE)
```
### Parameter `markdown`
The `markdown` parameter can be used in reports created with R Markdown. This will always print all rows:
```{r, echo = TRUE}
septic_patients %>%
select(hospital_id) %>%
freq(markdown = TRUE)
```
### Parameter `as.data.frame`
With the `as.data.frame` parameter you can assign the frequency table to an object, or just print it as a `data.frame` to the console:
```{r, echo = TRUE}
my_df <- septic_patients %>%
select(hospital_id) %>%
freq(as.data.frame = TRUE)
my_df
class(my_df)
```
----
```{r, echo = FALSE}
# this will print "2018" in 2018, and "2018-yyyy" after 2018.
yrs <- c(2018:format(Sys.Date(), "%Y"))
yrs <- c(min(yrs), max(yrs))
yrs <- paste(unique(yrs), collapse = "-")
```
AMR, (c) `r yrs`, `r packageDescription("AMR")$URL`
Licensed under the [GNU General Public License v2.0](https://github.com/msberends/AMR/blob/master/LICENSE).
+344
View File
@@ -0,0 +1,344 @@
<!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%2EfuLine truncated
</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. Let’s 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>