ct_to_community: Convert data to a community matrix

View source: R/ct_to_community.R

ct_to_communityR Documentation

Convert data to a community matrix

Description

The function transforms input data into a community matrix where rows represent sites, columns represent species, and values indicate the count or abundance of each species at each site.

Usage

ct_to_community(
  data,
  site_column,
  species_column,
  size_column,
  values_fill = NULL
)

Arguments

data

A data frame containing the input data.

site_column

The column in the data frame representing site identifiers. Can be specified as a string or unquoted column name.

species_column

The column in the data frame representing species identifiers. Can be specified as a string or unquoted column name.

size_column

(Optional) The column representing the size or abundance of the species at each site. If not provided, counts of species occurrences are calculated.

values_fill

(Optional) A value to fill missing cells in the resulting community matrix. Defaults to NULL.

Details

The function creates a site-by-species matrix suitable for ecological analysis. If size_column is not provided, the function counts occurrences of each species per site. If size_column is provided, its values are used as the measure for species abundance.

Value

A tibble where rows represent sites, columns represent species, and values represent the count or abundance of each species.

Examples

# Example data
df <- dplyr::tibble(
  site = c("A", "A", "B", "B", "C"),
  species = c("sp1", "sp2", "sp1", "sp3", "sp2"),
  abundance = c(5, 2, 3, 1, 4)
)

# Convert to community matrix with counts
ct_to_community(df, site_column = site, species_column = species)

# Convert to community matrix with abundance
ct_to_community(df, site_column = site, species_column = species, size_column = abundance)

# Fill missing cells with 0
ct_to_community(df, site_column = site, species_column = species, values_fill = 0)


ct documentation built on Oct. 22, 2026, 1:07 a.m.