Skip to content

Instantly share code, notes, and snippets.

@jhpoelen
Last active February 4, 2017 04:37
Show Gist options
  • Save jhpoelen/39d866721bb35a63d0e9b99073c5e8b2 to your computer and use it in GitHub Desktop.
Save jhpoelen/39d866721bb35a63d0e9b99073c5e8b2 to your computer and use it in GitHub Desktop.
lookup GBIF occurrence counts for species name
#install.packages('rgbif')
fresh <- read.csv('Fresh.species.csv')
#fresh <- data.frame(predator.taxon.name = c('Arius felis', 'Ariopsis felis', 'Gadus morhua'))
# appends columns gbifSpeciesKey, gbifOccCount when gbif knows about species and has occurrences
appendOccCount <- function(df) {
names <- df$predator.taxon.name
message('resolving gbif species keys')
df$gbifSpeciesKeys <- unlist(lapply(names, function(name) {
message('.', appendLF=FALSE);
taxon <- rgbif::name_backbone(name=name, rank='species')
if (taxon$matchType == 'NONE') {
NA
} else {
taxon$speciesKey
}
}))
message('retrieving occurrence counts for gbif species keys')
df$gbifOccCount <- unlist(lapply(df$gbifSpeciesKeys, function(key) {
message('.', appendLF=FALSE);
if (is.na(key)) {
NA
} else {
rgbif::occ_count(taxonKey=key)
}
}))
df
}
fresh.with.occ <- appendOccCount(fresh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment