knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4, fig.align = "center" )
fdth builds frequency distribution tables (fdt) and their associated
graphics from vectors, data frames, and matrices for both numerical and
categorical variables.
Core functions:
| Function | Purpose |
|---|---|
| fdt() | Frequency table for numerical data |
| fdt_cat() | Frequency table for categorical data |
| make.fdt() | Reconstruct a table from frequencies alone |
| make.fdt_cat() | Reconstruct a categorical table from frequencies |
| mfv() | Most frequent value (mode) |
| sd() / var() | Standard deviation / variance for grouped data |
library(fdth)
fdt()set.seed(42) x <- rnorm(200, mean = 10, sd = 2) ft <- fdt(x) ft
The default table has six columns:
| Column | Description |
|---|---|
| Class limits | Interval notation |
| f | Absolute frequency |
| rf | Relative frequency |
| rf(%) | Relative frequency (%) |
| cf | Cumulative frequency |
| cf(%) | Cumulative frequency (%) |
# Sturges (default) fdt(x, breaks = "Sturges") # Scott fdt(x, breaks = "Scott") # Freedman-Diaconis fdt(x, breaks = "FD") # Fixed number of classes fdt(x, k = 8)
# Fixed start, end and width ft2 <- fdt(x, start = 4, end = 16, h = 2) ft2
Use format.classes = TRUE together with pattern to control the number of
decimal places displayed in the class limits:
# Two decimal places print(ft, format.classes = TRUE, pattern = "%.2f") # Summary with the same formatting summary(ft, format.classes = TRUE, pattern = "%.2f")
By default intervals are left-closed [a, b). Use right = TRUE for
right-closed (a, b]:
fdt(x, right = TRUE)
x_na <- c(x, NA, NA) # This errors by design: tryCatch(fdt(x_na), error = function(e) message("Error: ", e$message)) # Remove NAs explicitly: fdt(x_na, na.rm = TRUE)
plot.fdt.default()All plot types are selected with the type argument.
plot(ft, type = "fh", main = "Frequency histogram") plot(ft, type = "fp", main = "Frequency polygon")
plot(ft, type = "rfh", main = "Relative frequency histogram") plot(ft, type = "rfph", main = "Relative frequency (%) histogram")
plot(ft, type = "d", main = "Density histogram")
plot(ft, type = "cfp", main = "Cumulative frequency polygon") plot(ft, type = "cfpp", main = "Cumulative frequency (%) polygon")
plot(ft, type = "fh", v = TRUE, v.round = 0, main = "Histogram with counts")
Once an fdt object exists, the usual statistics can be computed directly
from the grouped (tabulated) data — no access to the original vector is
needed.
ft3 <- fdt(x) mean(ft3) median(ft3) mfv(ft3) # mode(s) var(ft3) sd(ft3) # Quartiles (default) quantile(ft3) # Deciles quantile(ft3, i = 1:9, probs = seq(0, 1, 0.1))
fdt.data.frame()When the input is a data frame or matrix, fdt() builds one table
per numeric column and returns an fdt.multiple object.
ft_iris <- fdt(iris[, 1:4]) ft_iris
Use the by argument to stratify each numeric variable by a categorical
column:
ft_by <- fdt(iris[, c(1, 2, 5)], k = 5, by = "Species") ft_by
plot(ft_iris, type = "fh")
mean(ft_iris)
fdt_cat()set.seed(7) fruits <- sample(c("apple", "banana", "cherry", "strawberry", "melon"), size = 150, replace = TRUE) ft_cat <- fdt_cat(fruits) ft_cat
By default the table is sorted by descending frequency.
fdt_cat(fruits, sort = FALSE)
print(ft_cat, round = 3)
plot(ft_cat, type = "fb", main = "Frequency bar chart")
plot(ft_cat, type = "fd", main = "Frequency dotchart")
plot(ft_cat, type = "pa", main = "Pareto chart")
If the original data is no longer available but the frequency table is known,
make.fdt() and make.fdt_cat() rebuild complete fdt objects.
# Numerical ft_ref <- fdt(x) ft_new <- make.fdt(f = ft_ref$table$f, start = ft_ref$breaks["start"], end = ft_ref$breaks["end"]) print(ft_new, format.classes = TRUE, pattern = "%.2f")
# Categorical ft_new_cat <- make.fdt_cat(f = ft_cat$f, categories = ft_cat$Category) ft_new_cat
For publication-ready LaTeX tables use xtable::xtable() on any fdt
object. A dedicated vignette covers this workflow in detail:
vignette("latex_fdt", package = "fdth")
sessionInfo()
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.