ft_table: Count Character Frequencies in Factor Levels (Including NA...

View source: R/single_fct.R

ft_tableR Documentation

Count Character Frequencies in Factor Levels (Including NA Handling)

Description

Counts the frequency of each character appearing in the levels of a factor vector, optionally including NA values, and returns a table or vector.

Usage

ft_table(factor_vec, case = FALSE, include_na = FALSE, as_table = TRUE)

Arguments

factor_vec

A factor vector whose levels will be analyzed.

case

Logical. Should the character count be case-sensitive? Default is FALSE.

include_na

Logical. Should NA levels be included in the character count? Default is FALSE.

as_table

Logical. Should the result be returned as a table? If FALSE, a named vector is returned. Default is TRUE.

Value

A table or named vector of character frequencies.

Author(s)

Kai Guo

Examples

# Example factor vector with NA levels
factor_vec <- factor(c('apple', 'banana', NA, 'cherry', 'date', NA, 'fig', 'grape'), exclude = NULL)

# Get character frequencies (case-insensitive), excluding NA levels
ft_table(factor_vec)

# Include NA levels in the character frequencies
ft_table(factor_vec, include_na = TRUE)

fctutils documentation built on Oct. 1, 2024, 1:07 a.m.