#' Subsample a dataset by subcommunities
#'
#' Sample a dataset as if we only counted in some subcommunities.
#'
#' @param dataset A data frame containing abundance or incidence data
#' @param count The number of subcommunities to retain
#' @return The subsampled dataset as a tibble
#'
#' @export
#'
#' @examples
#' library(iNEXT)
#' data(bird)
#' sample_by_subcommunities(bird, count = 2)
#'
#' data(ciliates)
#' eto.incidence <- ciliates$EtoshaPan
#' sample_by_subcommunities(eto.incidence, count = 5)
#'
sample_by_subcommunities <- function(dataset, count)
{
cols <- ncol(dataset)
if (count > cols)
{
warning("Trying to pick more subcommunities than are present")
count = cols
}
sample_cols <- sample(cols, count)
ret <- dataset[, sample_cols]
if (count == 1)
{
ret <- as.data.frame(ret)
colnames(ret) <- colnames(dataset)[sample_cols]
rownames(ret) <- rownames(dataset)
}
return(tibble::as_data_frame(ret))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.