R/load_relevant.R

Defines functions load_relevant

Documented in load_relevant

#' Load the relevant occurrence data
#' @param min_occurrences The minimum number of occurrences per species.
#' @param min_species The minimum number of species recorded at the combination
#'   of location and year.
#' @export
#' @importFrom assertthat assert_that is.count
#' @importFrom dplyr filter group_by inner_join n transmute ungroup
#' @importFrom git2rdata read_vc
#' @importFrom tidyr pivot_wider
load_relevant <- function(min_occurrences = 1000, min_species = 3) {
  assert_that(is.count(min_occurrences), is.count(min_species))
  read_vc("occurrence", system.file(package = "ladybird")) %>%
    group_by(.data$taxon_key) %>%
    filter(n() >= min_occurrences) %>%
    group_by(.data$location, .data$year) %>%
    filter(n() >= min_species) %>%
    group_by(.data$taxon_key) %>%
    filter(n() >= min_occurrences) %>%
    group_by(.data$location, .data$year) %>%
    filter(n() >= min_species) %>%
    ungroup() %>%
    inner_join(
      read_vc("species", system.file(package = "ladybird")),
      by = "taxon_key"
    ) %>%
    transmute(
      .data$year, .data$location, species = .data$code, observed = 1L
    ) %>%
    pivot_wider(
      names_from = .data$species, values_from = .data$observed, values_fill = 0L
    )
}
inbo/ladybird documentation built on March 14, 2021, 3:47 p.m.