knitr::opts_chunk$set( collapse = TRUE, comment = "" )
library(simplefreqs)
This package provides a easy to produce frequency tables. It is built to print nicely both in the console and in knitted documents (i.e. in Rmarkdown or Quarto). It should produce attractive console, html, pdf, and (to a lesser extent) word output. We show basic console and HTML output below.
freq(iris, Species, markdown = FALSE)
If you have difficulty getting markdown to output correctly in your knitted document,
try setting results='asis'
in your Rmarkdown chunk options (or output='asis'
in your Quarto chunk options).
freq(iris, Species, markdown = TRUE)
It is designed to be fit into a piped/Tidyverse workflow.
library(dplyr)
Thus, the first parameter should be a data.frame (or tibble) and the second parameter an unquoted variable from the specified data.frame
data(storms) freq(storms, status)
or
storms |> freq(status)
Alternatively, freq
will detect if you pass a vector as the first parameter and
produce a frequency table from it.
freq(storms$category)
or
freq(storms[["category"]])
Results need not be printed. Results, instead, can be assigned to a variable. The results will be a data.fram with a class of both "simplefreqs_freq" and "data.frame".
df <- freq(mtcars, cyl) class(df)
This object will continue to print as a simplefreqs_freqs
object.
df <- freq(mtcars, carb) df
If needed, you can remove the "simplefreqs_freq" class and the object will print as a regular data.frame.
df <- freq(mtcars, carb) df <- as.data.frame(df) class(df) df
By default, a simple bar chart is produced alongside the frequency table when working interactively. You can control this with the plots parameter.
freq(iris, Species, plot = TRUE)
Plots can be supressed.
freq(iris, Species, plot = FALSE)
By default, results are sorted in descending order by frequency.
freq(mtcars, cyl, sort = TRUE)
Alternatively, sorting can be supressed. In that case, results will be sorted by the variables labels. For character variables, it will be alphabetic, for factors it will in order of the levels.
freq(mtcars, cyl, sort = FALSE)
NAs are, by default, included and treated as any other level of the variable.
df <- storms |> freq(category, na.rm = FALSE) df
Alternatively, NAs can be removed. In that case, NAs will not be included in the frequency table, but two relevent attribute will be set on the results. "na" will include the number of NAs removed and na_removed will be set to TRUE.
df <- storms |> freq(category, na.rm = TRUE) df attr(df, "na", exact = TRUE) attr(df, "na_removed", exact = TRUE)
simplefreqs::freq
tries to intuit if you are printing to the console
or are knitting a document. In some cases, you may need to directly specify the
type of output you want to display using the markdown
parameter. Set to FALSE
when you wish to print for the console.
freq(iris, Species, markdown = FALSE)
Set to markdown = TRUE
when you wish to include in a knitted document.
freq(iris, Species, markdown = TRUE)
Results can be optionally weighted by providing a vector of weight values.
freq(iris, Species, wt = Sepal.Length)
The printed results are highly customizable, both for console output and for HTML
output (PDF and Word output is less customizable). There are many available options
that control how these tables print. You can set these options per session (using
options(simplefreqs.OPTION = VALUE)
) or by adding the similar lines to your
project or global .RProfile file.
The exposed customization options are:
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.