ps_expand: Expand occupied-only results to full spatial extent

View source: R/utils.R

ps_expandR Documentation

Expand occupied-only results to full spatial extent

Description

Takes a matrix or vector computed on occupied sites only and expands it back to the full site grid, inserting NA for unoccupied sites. This is useful when performing custom analyses on ps$comm (which contains only occupied sites) and mapping the results back to the full raster or spatial object.

Usage

ps_expand(ps, x, spatial = FALSE)

Arguments

ps

A phylospatial object.

x

A matrix (or vector) with nrow(ps$comm) rows (i.e. one row per occupied site).

spatial

Logical: if TRUE, convert the expanded result to a spatial object using ps$spatial. Default is FALSE.

Value

If spatial = FALSE, a matrix with ps$n_sites rows and NA for unoccupied sites. If spatial = TRUE, a SpatRaster or sf object.

Examples

ps <- ps_simulate()

# custom analysis on the occupied-only community matrix
site_totals <- matrix(rowSums(ps$comm), ncol = 1)
colnames(site_totals) <- "total"

# expand to full extent as a matrix
ps_expand(ps, site_totals)

# expand and convert to spatial
ps_expand(ps, site_totals, spatial = TRUE)

phylospatial documentation built on April 4, 2026, 1:07 a.m.