## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.width = 6,
  fig.height = 4,
  fig.align = "center"
)

## ----load---------------------------------------------------------------------
library(fdth)

## ----fdt-basic----------------------------------------------------------------
set.seed(42)
x <- rnorm(200,
           mean = 10,
           sd = 2)

ft <- fdt(x)
ft

## ----fdt-breaks---------------------------------------------------------------
# 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)

## ----fdt-custom---------------------------------------------------------------
# Fixed start, end and width
ft2 <- fdt(x,
           start = 4,
           end   = 16,
           h     = 2)
ft2

## ----fdt-format---------------------------------------------------------------
# Two decimal places
print(ft,
      format.classes = TRUE,
      pattern        = "%.2f")

# Summary with the same formatting
summary(ft,
        format.classes = TRUE,
        pattern        = "%.2f")

## ----fdt-right----------------------------------------------------------------
fdt(x, right = TRUE)

## ----fdt-na-------------------------------------------------------------------
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-fh-fp, fig.show="hold", out.width="48%"-----------------------------
plot(ft, 
     type = "fh", 
     main = "Frequency histogram")
plot(ft, 
     type = "fp", 
     main = "Frequency polygon")

## ----plot-rf, fig.show="hold", out.width="48%"--------------------------------
plot(ft,
     type = "rfh",
     main = "Relative frequency histogram")
plot(ft,
     type = "rfph",
     main = "Relative frequency (%) histogram")

## ----plot-density-------------------------------------------------------------
plot(ft,
     type = "d",
     main = "Density histogram")

## ----plot-cf, fig.show="hold", out.width="48%"--------------------------------
plot(ft,
     type = "cfp",
     main = "Cumulative frequency polygon")
plot(ft,
     type = "cfpp",
     main = "Cumulative frequency (%) polygon")

## ----plot-labels--------------------------------------------------------------
plot(ft,
     type    = "fh",
     v       = TRUE,
     v.round = 0,
     main    = "Histogram with counts")

## ----stats--------------------------------------------------------------------
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-df-------------------------------------------------------------------
ft_iris <- fdt(iris[, 1:4])
ft_iris

## ----fdt-by-------------------------------------------------------------------
ft_by <- fdt(iris[, c(1, 2, 5)],
             k  = 5,
             by = "Species")
ft_by

## ----plot-multiple, fig.width=8, fig.height=6---------------------------------
plot(ft_iris, type = "fh")

## ----stats-multiple-----------------------------------------------------------
mean(ft_iris)

## ----fdt-cat-basic------------------------------------------------------------
set.seed(7)
fruits <- sample(c("apple", 
                   "banana", 
                   "cherry",
                   "strawberry",
                   "melon"),
                 size = 150,
                 replace = TRUE)

ft_cat <- fdt_cat(fruits)
ft_cat

## ----fdt-cat-nosort-----------------------------------------------------------
fdt_cat(fruits, sort = FALSE)

## ----fdt-cat-format-----------------------------------------------------------
print(ft_cat, round = 3)

## ----plot-cat-bar-------------------------------------------------------------
plot(ft_cat,
     type = "fb",
     main = "Frequency bar chart")

## ----plot-cat-dotchart--------------------------------------------------------
plot(ft_cat,
     type = "fd",
     main = "Frequency dotchart")

## ----plot-cat-pareto----------------------------------------------------------
plot(ft_cat,
     type = "pa",
     main = "Pareto chart")

## ----make-fdt-----------------------------------------------------------------
# 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")

## ----make-fdt-cat-------------------------------------------------------------
# Categorical
ft_new_cat <- make.fdt_cat(f = ft_cat$f,
                           categories = ft_cat$Category)
ft_new_cat

## ----xtable-ref, eval=FALSE---------------------------------------------------
# vignette("latex_fdt", package = "fdth")

## ----session------------------------------------------------------------------
sessionInfo()

