compute_mode: Compute the Mode of a Numeric Vector

View source: R/stats.R

compute_modeR Documentation

Compute the Mode of a Numeric Vector

Description

This function calculates the mode (the value with the highest frequency) of a numeric vector. It supports handling of missing values through the na.rm argument.

Usage

compute_mode(x, na.rm = TRUE)

Arguments

x

A numeric vector for which the mode is to be calculated.

na.rm

A logical value indicating whether to remove NA values before calculating the mode. Default is TRUE.

Value

A numeric vector containing the mode(s) of the input vector. If the input vector is empty or only contains NA values, the function returns NA. If there are multiple modes (a tie), the function returns all modes as a vector.

Examples

# Basic usage
compute_mode(c(1, 2, 2, 3, 4, 4, 4, 5))

# Handle missing values
compute_mode(c(1, 2, 2, NA, 3, 4, 4, 4, 5, NA))

# Include \code{NA} values in mode calculation
compute_mode(c(1, 2, 2, NA, 3, 4, 4, 4, 5, NA), na.rm = FALSE)

# Edge cases
compute_mode(numeric(0))  # Returns NA for empty vector
compute_mode(c(NA, NA, NA), na.rm = TRUE)  # Returns NA for all NAs


Lightbridge-KS/lbx documentation built on Dec. 4, 2024, 2:53 a.m.