Learn R Programming

PNC (version 0.1.0)

extract_traits: Extract Plant Traits from Trait Database

Description

This function extracts plant trait data from the TRY database or similar datasets for a specified list of taxa at different taxonomic ranks (species, genus, or family). For numeric traits at genus and family levels, it calculates mean values across all available records.

Usage

extract_traits(sp.list, dataset, rank = "species", traits = NULL)

Value

A data frame with taxa names as row names and trait names as column names. For species-level extraction, returns the first occurrence of each species. For genus/family-level extraction, returns mean values for numeric traits and the first occurrence for non-numeric traits. Missing values are represented as NA.

Arguments

sp.list

A character vector containing the names of taxa to extract traits for. The names should match the taxonomic rank specified in the 'rank' parameter.

dataset

A data frame containing trait data. Default is TRY database. Must contain columns named "species", "genus", and "family" for taxonomic information.

rank

A character string specifying the taxonomic rank to match against. Must be one of "species", "genus", or "family". Default is "species".

traits

A character vector specifying which traits to extract. If NULL (default), all available traits in the dataset will be extracted. Available traits are all columns except "species", "genus", and "family".

Details

The function performs the following operations:

  • Validates input parameters

  • Identifies available traits in the dataset

  • Matches input taxa with dataset entries

  • Reports missing taxa

  • Extracts trait data based on the specified taxonomic rank

  • For numeric traits at genus/family level, calculates mean values

  • For non-numeric traits, uses the first available value

  • Handles NaN values by converting them to NA

Examples

Run this code
# Load the dataset
data(TRY)

# Extract all traits for species
species_list <- c("Acaena novae-zelandiae", "Adiantum capillus-veneris", "Zuelania guidonia")
extract_traits(species_list, TRY, rank = "species")

# Extract specific traits for species
extract_traits(species_list, TRY, rank = "species",
               traits = c("LA", "LMA", "LeafN", "PlantHeight", "SeedMass", "SSD"))

# Extract specific traits at genus level
genus_list <- c("Acaena", "Adiantum")
extract_traits(genus_list, TRY, rank = "genus",
               traits = c("LDMC", "PlantHeight", "SeedMass"))

Run the code above in your browser using DataLab