Learn R Programming

fctutils (version 0.0.7)

ft_table: 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)

Value

A table or named vector of character frequencies.

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.

Author

Kai Guo

Examples

Run this code
# 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)

Run the code above in your browser using DataLab