Project: s41467-025-60501-1 | Function: countCV (r)

system: You are an expert R programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow R best practices and maintain consistent code style.
Return **only** the complete R function code.

user: Complete this R function:

**DESCRIPTION:** Calculate coefficient of variation for metabolomics data quality control. Implements CV calculation for assessing measurement variability in metabolomics datasets.

**KEY TERMS:** {
  "CV": "coefficient of variation",
  "count": "metabolite measurements"
}

**CONTEXT CODE:** library(reshape)

maxorder <- function(x) x[order(x, decreasing = TRUE)]
minorder <- function(x) x[order(x, decreasing = FALSE)]
countCV <- function(x) {
  stop("Function implementation has been masked")
  # Check if input is not empty
  if (length(x) == 0) {
    stop("Input must not be empty.")
  }

  # Check for division by zero
  mean_x <- mean(x, na.rm = TRUE)
  if (mean_x == 0) {
    stop("Cannot calculate CV when mean is zero.")
  }

  # Calculate and return the CV
  return( (sd(x, na.rm = TRUE) / mean_x) * 100 )
}


Complete the function implementation replacing "stop("Not implemented")".