Skip to content

Instantly share code, notes, and snippets.

@PhillRob
Last active January 18, 2017 04:36
Show Gist options
  • Save PhillRob/ed9d919afc6af6163634f7dbaff8968e to your computer and use it in GitHub Desktop.
Save PhillRob/ed9d919afc6af6163634f7dbaff8968e to your computer and use it in GitHub Desktop.
gets the first occurrence record per speciesKey and ISO2 country code from gbif.org based on eventDate
# load rgbif package
install.packages("rgbif"); library(rgbif)
# create a matrix with one line per query
country <- c('AU', 'US', 'DE') #iso2 country code
species <- c(2768885, 3020791) # alternativly get the gbif speciesKeys based on species names
sp <- c("Asparagus officinalis", "Prunus avium")
species <- sapply(sp, function(x) name_backbone(x, rank="plants")$speciesKey)
m <- rbind(cbind(country[1], species), cbind(country[2], species), cbind(country[3], species))
# function to get the number first record per species and country based on eventDate
first.record <- function(species, country, fields, limit = 200) {
a <- as.matrix(occ_search(
taxonKey = species,
limit = limit,
country = country,
return = 'data',
fields = c("name","key","publishingOrgKey" , "publishingCountry" ,
"basisOfRecord" ,"taxonKey","speciesKey","scientificName",
"species","decimalLongitude","decimalLatitude" ,"year","month",
"day","eventDate","issues","countryCode","institutionCode")))# add remove fields if required
a <- a[!duplicated(a),] #dubs
a <- a[!duplicated(a[, "eventDate"]),] #event date dubs
min <- min(a[, "eventDate"], na.rm = t)
a <- a[a[, "eventDate"] == min]
}
# run the function through the matrix and change the limit to get the max. occurences allowed by the GBIF API
r <- apply(X = m, MARGIN = 1, function(x) first.record(species = x[2], country = x[1], limit = 200000))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment