Combining occurrence data from rgbif::occ_data/occ_search

,

An rgbif user asked how to combine occurrence data from occ_search(), including how to deal with taxa that have no occurrence data results

Make a species list and pass it to occ_data()

library("rgbif")
spp <- c("Abrodictyum laetum", "Abrodictyum brassii", "Abrodictyum boninense")
x <- occ_data(scientificName = spp, hasCoordinate=TRUE, limit = 10)
x

Looking at the output, one of the three taxa had no occurrence data results, and two did.

x$`Abrodictyum boninense`$data
#> NULL

Combine data using dplyr::bind_rows() (similarly, you can use data.table::rbindlist). Either of these options will be able to handle NULL's, and will fill columns so that if data.frame’s have different sets of column names they will still bind together.

library("dplyr")
bind_rows(lapply(x, "[[", "data"))
#> # A tibble: 18 x 86
#>    key   scientificName decimalLatitude decimalLongitude issues datasetKey publishingOrgKey networkKeys installationKey
#>    <chr> <chr>                    <dbl>            <dbl> <chr>  <chr>      <chr>            <list>      <chr>
#>  1 4373… Trichomanes l…           -22.0             167. gass84 b5cdf794-… 2cd829bb-b713-4… <list [0]>  86c2834b-fcd6-…
#>  2 4379… Trichomanes l…           -21.7             166. gass84 b5cdf794-… 2cd829bb-b713-4… <list [0]>  86c2834b-fcd6-…
#>  3 1897… Abrodictyum l…           -22.1             166. cdrou… 83ae84cf-… 32e82a64-c9b4-4… <list [0]>  58f76e14-f21d-…
#>  4 1258… Trichomanes l…           -21.2             165. gass84 7bd65a7a-… 90fd6680-349f-1… <list [0]>  c4e134d3-c68f-…
#>  5 9311… Trichomanes l…           -21.9             166. cdrou… 4443b1f2-… a6de8f30-d931-1… <list [0]>  791e3715-7452-…
#>  6 9311… Trichomanes l…           -21.9             166. cdrou… 4443b1f2-… a6de8f30-d931-1… <list [0]>  791e3715-7452-…
#>  7 9311… Trichomanes l…           -21.9             166. cdrou… 4443b1f2-… a6de8f30-d931-1… <list [0]>  791e3715-7452-…
#>  8 1258… Trichomanes l…           -21.9             166. gass84 7bd65a7a-… 90fd6680-349f-1… <list [0]>  c4e134d3-c68f-…
#>  9 9961… Abrodictyum b…           -16.9             146. ""     4ce8e3f9-… 1deb09e1-ded6-4… <list [0]>  19893c10-381e-…
#> 10 9962… Abrodictyum b…           -16.9             146. ""     4ce8e3f9-… 1deb09e1-ded6-4… <list [0]>  19893c10-381e-…
#> 11 9940… Abrodictyum b…           -16.1             145. ""     4ce8e3f9-… 1deb09e1-ded6-4… <list [0]>  19893c10-381e-…
#> 12 9940… Abrodictyum b…           -16.5             145. ""     4ce8e3f9-… 1deb09e1-ded6-4… <list [0]>  19893c10-381e-…
#> 13 1652… Abrodictyum b…           -16.2             145. cdrou… 5d7f5915-… 96835ce2-d3af-4… <list [0]>  19893c10-381e-…
#> 14 1666… Abrodictyum b…           -16.2             145. cdrou… e4473544-… 3c5e4331-7f2f-4… <list [0]>  19893c10-381e-…
#> 15 9940… Abrodictyum b…           -16.2             145. ""     4ce8e3f9-… 1deb09e1-ded6-4… <list [0]>  19893c10-381e-…
#> 16 1652… Abrodictyum b…           -17.2             146. cdrou… 5d7f5915-… 96835ce2-d3af-4… <list [0]>  19893c10-381e-…
#> 17 1667… Abrodictyum b…           -17.2             146. cdrou… e4473544-… 3c5e4331-7f2f-4… <list [0]>  19893c10-381e-…
#> 18 1666… Abrodictyum b…           -18.9             146. cdrou… e4473544-… 3c5e4331-7f2f-4… <list [0]>  19893c10-381e-…
#> # … with 77 more variables: publishingCountry <chr>, protocol <chr>, lastCrawled <chr>, lastParsed <chr>,
#> #   crawlId <int>, basisOfRecord <chr>, taxonKey <int>, kingdomKey <int>, phylumKey <int>, classKey <int>,
#> #   orderKey <int>, familyKey <int>, genusKey <int>, speciesKey <int>, acceptedTaxonKey <int>,
#> #   acceptedScientificName <chr>, kingdom <chr>, phylum <chr>, order <chr>, family <chr>, genus <chr>, species <chr>,
#> #   genericName <chr>, specificEpithet <chr>, taxonRank <chr>, taxonomicStatus <chr>, stateProvince <chr>, year <int>,
#> #   month <int>, day <int>, eventDate <chr>, modified <chr>, lastInterpreted <chr>, license <chr>, geodeticDatum <chr>,
#> #   class <chr>, countryCode <chr>, country <chr>, preparations <chr>, identifier <chr>, recordedBy <chr>,
#> #   catalogNumber <chr>, institutionCode <chr>, fieldNotes <chr>, fieldNumber <chr>, gbifID <chr>,
#> #   verbatimLocality <chr>, collectionCode <chr>, occurrenceID <chr>, identifiedBy <chr>, elevation <dbl>,
#> #   references <chr>, higherGeography <chr>, language <chr>, verbatimElevation <chr>, type <chr>,
#> #   individualCount <int>, continent <chr>, rightsHolder <chr>, nomenclaturalStatus <chr>, recordNumber <chr>,
#> #   nomenclaturalCode <chr>, datasetName <chr>, locality <chr>, taxonID <chr>, ownerInstitutionCode <chr>,
#> #   bibliographicCitation <chr>, collectionID <chr>, institutionID <chr>, locationRemarks <chr>, name <chr>,
#> #   coordinateUncertaintyInMeters <dbl>, occurrenceStatus <chr>, taxonConceptID <chr>, occurrenceRemarks <chr>,
#> #   depth <dbl>, eventID <chr>

The above works similarly for occ_search().

1 Like