R/lets_subsetPAMatt.R

Defines functions lets.subsetPAMatt

Documented in lets.subsetPAMatt

#' Subset an attribute PAM by species names
#'
#' Subsets an attribute-based presence-absence matrix (PAM) generated by
#' \code{\link{lets.attrpam}} by retaining only a selected set of species.
#' Species not included in \code{names} are removed from the PAM, while the
#' first three columns of the PAM table are always preserved because they store
#' attribute-space cell metadata. After subsetting, species richness is
#' recalculated for each attribute-space cell and written back to the associated
#' raster object.
#'
#' Optionally, attribute-space cells with zero richness after subsetting can be
#' removed from the tabular component of the PAM.
#'
#' @param x A PAM in attribute space produced by \code{\link{lets.attrpam}}.
#'   This object is expected to be a list in which:
#'   \itemize{
#'     \item \code{x[[1]]} is a data frame containing attribute-space cells in
#'     rows, the first three columns as cell metadata, and species occurrences
#'     in the remaining columns.
#'     \item \code{x[[2]]} is a \code{\link[terra:SpatRaster-class]{terra::SpatRaster}}
#'     representing the attribute-space grid.
#'   }
#' @param species A character vector with species names to retain in the PAM.
#' @param remove.cells Logical. If \code{TRUE}, attribute-space cells with zero
#'   richness after subsetting are removed from the data-frame component
#'   (\code{x[[1]]}). Default is \code{TRUE}.
#'
#' @return A list with the same structure as the input object:
#' \itemize{
#'   \item \code{x[[1]]}: the subsetted attribute PAM table.
#'   \item \code{x[[2]]}: the attribute-space raster with richness values updated
#'   according to the retained species.
#' }
#'
#' @details
#' The function assumes that the first three columns of \code{x[[1]]} contain
#' non-species information and therefore always preserves them during
#' subsetting. Richness is recalculated as the row sum across all retained
#' species columns.
#'
#' If none of the supplied species names match the species present in the PAM,
#' the function stops with an error.
#'
#' @examples
#' \dontrun{
#' n <- 2000
#' Species <- paste0("sp", 1:n)
#' trait_a <- rnorm(n)
#' trait_b <- trait_a * 0.2 + rnorm(n)
#' x <- data.frame(Species, trait_a, trait_b)
#'
#' pam_att <- lets.attrpam(x, n_bins = 30)
#' names <- sample(Species, 200)
#' pam_sub <- lets.subsetPAMatt(pam_att, names)
#' lets.plot.attrpam(pam_sub)
#' }
#'
#' @seealso \code{\link{lets.attrpam}}, \code{\link{lets.plot.attrpam}}
#'
#' @export

lets.subsetPAMatt <- function(x, species, remove.cells = TRUE) {
  sps <- colnames(x[[1]])
  keep <- sps %in% species
  keep[1:3] <- TRUE
  
  
  x[[1]] <- x[[1]][, keep, drop = FALSE]
  rich <- as.numeric(rowSums(x[[1]][, -(1:3), drop = FALSE]))
  cells <- terra::cellFromXY(x[[2]], x[[1]][, 2:3, drop = FALSE])
  terra::values(x[[2]])[cells] <- rich
  
  if (remove.cells) {
    x[[1]] <- x[[1]][rich > 0, , drop = FALSE]
  }
  
  return(x)
}

Try the letsR package in your browser

Any scripts or data that you put into this service are public.

letsR documentation built on Nov. 26, 2026, 9:09 a.m.