R/sample_by_species.R

#' Subsample a dataset by species
#'
#' Sample a dataset as if we only recorded some species.
#'
#' @param dataset A data frame containing abundance or incidence data
#' @param count The number of species to retain
#' @return The subsampled dataset as a tibble
#'
#' @export
#'
#' @examples
#' library(iNEXT)
#' data(bird)
#' sample_by_species(bird, count = 2)
#'
#' data(ciliates)
#' eto.incidence <- ciliates$EtoshaPan
#' sample_by_species(eto.incidence, 5)
#'
sample_by_species <- function(dataset, count)
{
  rows <- nrow(dataset)
  if (count > rows)
  {
    warning("Trying to pick more species than are present")
    count = rows
  }

  return(tibble::as_data_frame(dataset[sample(rows, count), ]))
}
boydorr/sampling documentation built on May 23, 2019, 1:45 p.m.