R/var_mean.R

Defines functions var_mean

Documented in var_mean

#' @title Variance/mean ratio
#' @description  Aggregation Index - Variance/mean
#' @param x number of parasites by host
#' @export
#' @examples
#' attach(birdticks)
#' var_mean(ticks)


var_mean <- function(x) {
  if (any(is.na(x))){
    warning("Missing values were ignored.")
    x <- x[!is.na(x)]
  }
  if (!is.numeric(x)){
    stop(paste("Number of parasites must be a numeric variable."))
  }
  if (any(x < 0)) {
    stop(paste("Number of parasites must be positive values."))
  }
  if (any(!is.wholenumber(x))) {
    stop(paste("Numbers of parasites must be integers."))
  }
  if (sum(x) == 0) {
    return(NA)
  } else {
    I <- round(var(x)/mean(x), digits = 3)
    return(I)
  }
}
ralphmp/ecopaR documentation built on May 27, 2023, 3:50 p.m.