R/select_observed_range.R

Defines functions select_observed_range

Documented in select_observed_range

#' Limit the observation to the range in which the species is present
#' @inheritParams select_factor_threshold
#' @export
#' @examples
#' observation <- data.frame(
#'   Count = c(0, 0, 100, 101, 0, 51, 1, 0, 0, 0),
#'   Year = 1:10
#' )
#' select_observed_range(observation, "Year")
#' @importFrom n2khelper check_character check_dataframe_variable
#' @importFrom stats na.fail
select_observed_range <- function(observation, variable) {
  variable <- check_character(
    x = variable,
    name = "variable",
    na_action = na.fail
  )
  check_dataframe_variable(
    df = observation,
    variable = c("Count", variable),
    name = "observation",
    error = TRUE
  )
  if (any(is.na(observation[, variable]))) {
    warning(
      variable, " contains missing values. Corresponding rows are removed."
    )
  }

  observed_range <- range(
    observation[observation$Count > 0, variable],
    na.rm = TRUE
  )
  selection <- observed_range[1] <= observation[, variable] &
    observation[, variable] <= observed_range[2]
  return(observation[selection, ])
}
inbo/n2kanalysis documentation built on Feb. 17, 2024, 1:32 a.m.