Learn R Programming

eventreport (version 0.1.1)

calc_mode_na_ignore: Calculate mode with optional tie-breaks ignoring NA and empty strings

Description

This function calculates the mode of a given vector, ignoring `NA` and empty strings, and optionally resolves ties using one or two levels of tie-breaks. If all values are `NA` or empty, the function returns `NA`.

Usage

calc_mode_na_ignore(x, tie_break = NULL, second_tie_break = NULL)

Value

Returns the mode of `x` ignoring `NA` and empty strings. If the filtered vector is empty or all elements are `NA` or empty, returns `NA`.

Arguments

x

A character vector for which to find the mode.

tie_break

An optional numeric vector used as the first tie-break criterion.

second_tie_break

An optional numeric vector used as the second tie-break criterion when the first is insufficient.

Examples

Run this code
data <- c("apple", "", "banana", NA)
tie_break <- c(1, NA, 1, NA)
second_tie_break <- c(1, NA, 2, NA)
calc_mode_na_ignore(data)  # Expect: "apple"
calc_mode_na_ignore(data, tie_break)  # Expect: "banana"
calc_mode_na_ignore(data, tie_break, second_tie_break)  # Expect: "banana"

Run the code above in your browser using DataLab