mfdb_helpers.Rd
Misc. functions to aid working with an MFDB database.
# Find species from abbreviated names
mfdb_find_species(partial_name, single_matches_only = FALSE)
Vector of partial species names, e.g. "Gad Mor", "gad. Mor.", "Gadus Mor", will all match "Cod (Gadus Morhua)".
Logical, default FALSE. If true, return NA for partial_names with multiple or zero matches.
A matrix of all potential id, name & descriptions for each item in
partial_name
.
mfdb_find_species(c("gad mor", "tube worms"))
#> gad mor tube worms
#> id 8791030402 1e+10
#> name "COD" "TBX"
#> description "Cod (Gadus Morhua)" "Tube Worms (Tubeworms)"
# gad mor tube worms
# id 8791030402 1e+10
# name "COD" "TBX"
# description "Cod (Gadus Morhua)" "Tube Worms (Tubeworms)"
# Can also generate a map to help insert a data.frame of foreign data
stomachs <- read.csv(text = '
stomach_name,species,digestion_stage,length,weight,count
A,Palaemon Elegans,1,1,10,5
A,Palaemon Elegans,1,4,40,1
B,Palaemon Elegans,1,1,10,5
B,Palaemon Elegans,4,1,10,5
B,Palaemon Elegans,5,1,10,NA
B,Palaemon Elegans,5,1,10,NA
C,Crangon Crangon,2,3.5,9.5,3
D,Palaemon Elegans,1,1.4,10,1
D,Crangon Crangon,5,4,40,1
E,Worms,1,1.4,10,1
', stringsAsFactors = TRUE)
# Work out a map from all Prey_Species_Name values to MFDB species codes
species_map <- mfdb_find_species(levels(stomachs$species), single_matches_only = TRUE)['name',]
# Put the new levels back onto the species column
levels(stomachs$species) <- unlist(species_map)
stomachs
#> stomach_name species digestion_stage length weight count
#> 1 A PSQ 1 1.0 10.0 5
#> 2 A PSQ 1 4.0 40.0 1
#> 3 B PSQ 1 1.0 10.0 5
#> 4 B PSQ 4 1.0 10.0 5
#> 5 B PSQ 5 1.0 10.0 NA
#> 6 B PSQ 5 1.0 10.0 NA
#> 7 C CSH 2 3.5 9.5 3
#> 8 D PSQ 1 1.4 10.0 1
#> 9 D CSH 5 4.0 40.0 1
#> 10 E <NA> 1 1.4 10.0 1