Remove species (rows) from a trait matrix that have fewer than `threshold` % of traits available. Returns filtered data frame with a report.
filter_completeness <- function(df, threshold = 50, cols = NULL) {
if (is.null(cols)) cols <- names(df)[sapply(df, is.numeric)]
pct <- rowMeans(!is.na(df[, cols])) * 100
keep <- pct >= threshold
message(sprintf("Kept %d / %d species (>= %.0f%% complete)",
sum(keep), nrow(df), threshold))
df[keep, ]
}
# Example
filter_completeness(iris, threshold = 80)