fdt_cat: Frequency distribution table for categorical data

fdt_catR Documentation

Frequency distribution table for categorical data

Description

An S3 set of methods to easily create categorical frequency distribution tables (‘⁠fdt_cat⁠’) from vector, data.frame and matrix objects.

Usage

## S3 generic
fdt_cat(x, ...)

## S3 methods
## Default S3 method:
fdt_cat(x,
        sort = TRUE,
        decreasing = TRUE, ...)

## S3 method for class 'data.frame'
fdt_cat(x,
        by,
        sort = TRUE,
        decreasing = TRUE, ...) 

## S3 method for class 'matrix'
fdt_cat(x,
        sort = TRUE,
        decreasing = TRUE, ...)

Arguments

x

a vector, data.frame or matrix object. If ‘⁠x⁠’ is data.frame or matrix it must contain at least one character/factor column.

by

categorical variable used for grouping each categorical response, useful only on data.frame.

sort

logical. Should the fdt_cat be sorted by the absolute frequency into ascending or descending order? (default = TRUE).

decreasing

logical. Should the sort order be increasing or decreasing? (default = TRUE).

...

optional further arguments (required by generic).

Details

The simplest way to run ‘⁠fdt_cat⁠’ is supplying only the ‘⁠x⁠’ object, for example: ct <- fdt_cat(x). In this case all necessary default values (‘⁠sort = TRUE⁠’ and ‘⁠decreasing = TRUE⁠’) will be used.

These options make the ‘⁠fdt_cat⁠’ very easy and flexible.

The ‘⁠fdt_cat⁠’ object stores information to be used by methods summary, print, plot and mfv. The result of plot is a bar plot. The methods summary.fdt_cat, print.fdt_cat and plot.fdt_cat provide a reasonable set of parameters to format and plot the ‘⁠fdt_cat⁠’ object in a clear (and publishable) way.

Value

For fdt_cat the method fdt_cat.default returns a data.frame storing the ‘⁠fdt⁠’.

The methods fdt_cat.data.frame and fdt_cat.matrix return a list of class fdt_cat..multiple. This list has one slot for each categorical variable of the supplied ‘⁠x⁠’. Each slot, corresponding to each categorical variable, stores the same slots of the fdt_cat.default described above.

Author(s)

Faria, J. C.
Allaman, I. B
Jelihovschi, E. G.

See Also

hist provided by graphics and table, cut both provided by base.

Examples

library(fdth)

# Categorical
x <- sample(x = letters[1:5],
            size = 5e2,
            rep = TRUE)

table(x)
sum(table(x))

(ft.c <- fdt_cat(x))

(ft.c <- fdt_cat(x,
                 sort = FALSE))

#=========================================================
# Data.frame: multivariate with two categorical variables
#=========================================================
mdf <- data.frame(c1 = sample(LETTERS[1:3],
                              1e2,
                              rep = TRUE),
                  c2 = as.factor(sample(1:10,
                                        1e2,
                                        rep = TRUE)),
                  n1 = c(NA,
                         NA,
                         rnorm(96,
                               10,
                               1),
                         NA,
                         NA),
                  n2 = rnorm(100,
                             60,
                             4),
                  n3 = rnorm(100,
                             50,
                             4),
                  stringsAsFactors = TRUE)

str(mdf)

(ft.c <- fdt_cat(mdf))

(ft.c <- fdt_cat(mdf,
                 dec = FALSE))

(ft.c <- fdt_cat(mdf,
                 sort = FALSE))

(ft.c <- fdt_cat(mdf,
                 by = 'c1'))

#===================================
# Matrix: two categorical variables
#===================================
x <- matrix(sample(x = letters[1:10],
                   size = 100,
                   rep = TRUE),
            nc = 2,
            dimnames = list(NULL,
                            c('c1', 'c2')))

head(x)

(ft.c <- fdt_cat(x))

fdth documentation built on May 26, 2026, 1:06 a.m.