knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(naturecounts)

In order to download data pertaining to specific species, you'll have to get the species id codes. These are numeric codes that reflect species identity, and in complex cases, take account of subspecies or changes to species identity.

For all species, you can search by scientific, English or French name with the search_species() function.

For birds, you can also search by alphanumeric species codes with the search_species_code() function. Note: These alphanumeric codes are not the ones used by naturecounts, but are ones used by various taxonomic authorities. This function also gives you the option of returning all species codes (including subspecies, etc.) related to a single species, and is considered a more robust method for ensuring that you do not miss observations.

The following examples use the "testuser" user which is not available to you. You can quickly sign up for a free account of your own to access and play around with these examples. Simply replace testuser with your own username.

Searching by name

The simplest way to determine a species id (and the only way, for non-avian taxa) is to use the search_species() function which searches for a species id according to scientific, English or French name and returns a data frame of taxonomic information for all related hits:

search_species("moose")
search_species("swallowtail")
search_species("mesange a tete noire")

These species ids can then be used to download data either directly:

moose <- nc_data_dl(species = 133990, verbose = FALSE, 
                    username = "testuser", info = "nc_vignette")

Or by saving and referencing the data frame:

moose_id <- search_species("moose")$species_id
moose <- nc_data_dl(species = moose_id, verbose = FALSE, 
                    username = "testuser", info = "nc_vignette")

This might be considered overkill for a single species, but is useful when you want to download data for multiple species:

chickadee_ids <- search_species("chickadee")
chickadee_ids

# Let's take the first three
chickadees <- nc_data_dl(species = chickadee_ids$species_id[1:3], verbose = FALSE, 
                         username = "testuser", info = "nc_vignette")

Searching by alphanumeric code

Taxonomy for bird species has long included various alphanumeric codes from different taxonomic authorities. The codes often include separate codes for recognized subdivisions of the species (subspecies, subpopulations, hybrids, etc.). The search_species_code() allows you to search for avian species ids according to these authorities.

Authorities

The default taxonomic authority used is BSCDATA, which uses 4-letter alpha codes:

search_species_code("BCCH")

The search function is case insensitive:

search_species_code("bcch")

However, you can search by codes for different authorities, by specifying the appropriate authority argument:

search_species_code(8868, authority = "CBC")

All related subspecies

By default, the search_species_codes() returns all species ids related to the search term.

For example, searching for myrtle warblers (MYWA), one of two subspecies of the yellow-rumped warbler (YRWA) returns both subspecies and the parent species.

search_species_code("MYWA")

Note: Different taxonomic authorities recognize different subgroups.

For example, BSCDATA recognizes 4 groups for dark-eyed juncos:

search_species_code("DEJU")

Whereas CBC also recognizes sub group hybrids (and the Guadalupe junco):

search_species_code("12385", authority = "CBC")

You can search by more than one authority at the same time. Note that your search term only needs to match one authority (not both), and that the information returned reflects both authorities combined.

search_species_code("DEJU", authority = c("BSCDATA", "CBC"))

Exact species

If you do not want all subgroups, you can use the results = "exact" argument to return only an exact match.

search_species_code("DEJU", results = "exact")
search_species_code("ORJU", results = "exact")

Advanced searches

If you have a very specific type of search you'd like to do, you may be better off using the filter() function from the dplyr package.

library(dplyr)

For example, let's assume you're interested in collecting species codes for in the Muridae family.

We can get the entire species list by omitting a search term, and also return all taxonomic information available with the show = "all" argument.

all <- search_species(show = "all")
head(all)

Now we can search for the family

mice_ids <- filter(all, family_name == "Muridae")
head(mice_ids)
length(mice_ids$species_id)

So there are a lot of muridae species! But probably not that many in the actual database. We can download all available observations of these species, by passing the species_ids to the nc_data_dl() function.

mice <- nc_data_dl(species = mice_ids$species_id, 
                   verbose = FALSE, username = "testuser", info = "nc_vignette")
mice

Actually more than I expected (although not many compared to bird species).



BirdStudiesCanada/rNatureCounts documentation built on July 3, 2023, 2:06 a.m.