Last active
February 4, 2017 04:37
-
-
Save jhpoelen/39d866721bb35a63d0e9b99073c5e8b2 to your computer and use it in GitHub Desktop.
lookup GBIF occurrence counts for species name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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