Skip to content

Instantly share code, notes, and snippets.

@karthik
Last active August 16, 2016 16:40
Show Gist options
  • Save karthik/94bcc4abdd511d61ac0aecca70b20761 to your computer and use it in GitHub Desktop.
Save karthik/94bcc4abdd511d61ac0aecca70b20761 to your computer and use it in GitHub Desktop.
Getting fisheries data
library(rfisheries)
library(dplyr)
slist <- of_species_codes()
# This function fails with NULL quietly when there is no data for a particular species.
# However, if there is data, it will return the landings information
land_data <- dplyr::failwith(NULL, of_landings, quiet = TRUE)
# e.g.
# land_data(species = "COD")
# will work
# But
# land_data(species = "ZZZ")
# will not
# Now to run this on a small subset of 25 random species
small_species_list <- slist[sample(1:nrow(slist), 25), ]
# For each species, we get data, or return NULL if nothing is found
results <- lapply(small_species_list$a3_code, function(x) {
land_data(species = x)
})
# Now we can remove those list items that don't have data (i.e. NULL)
clean_results <- Filter(Negate(function(x) is.null(unlist(x))), results)
# At this point you can do furhter things with the list, or
# compact everything to a data.frame and write that as csv.
results_data_frame <- bind_rows(clean_results)
# To see how many speices have data in the results
results_data_frame %>% distinct(species)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment