Downloading and Pruning Calochortus Species Occurence Data

Author

Julianna J. Harden

Published

April 24, 2024

1 Set your working directory to the folder you want your output to be located

setwd()

2 Install and load necessary packages

#install.packages("rgbif")
library(rgbif)
#install.packages("CoordinateCleaner")
library(CoordinateCleaner)

3 Download GBIF occurrence data

The Global Biodiversity Information Facility (GBIF) is an international network and data infrastructure that aims to provide open access to data about all types of life on Earth. This step will allow you to retrieve occurrence data and the associated metadata for the species of interest.

download_calochortus_occ <- function(Calochortus)

3.1 Search for occurrences of the genus

  genus_key <- name_backbone(Calochortus)$usageKey
  occ_search <- occ_search(taxonKey = genus_key, limit = 100000) # Here you can change limit as needed

3.2 Using this code, you can retrieve your occurrence data

 occ_data <- occ_search$data
  return(occ_data)

3.2.1 Define the genus name

Calochortus <- "Calochortus"

3.3 Download occurrence data for the genus

occ_data <- download_calochortus_occ(Calochortus)

4 Filter occurrence data using CoordinateCleaner:

This step is important for checking the validity of the coordinates extracted from GBIF.

cleaned_occ_data <- clean_coordinates(occ_data)

In my case, I only wished to keep the columns for the scientific name, longitude, and latitude from the dataset retreived from GBIF.

calo_occ <- occ_data[, c("scientificName", "decimalLatitude", "decimalLongitude")]

5 List of row numbers to be deleted based on the output from CoordinateCleaner

Use the output from the “cleaned_coordinates” step as the numbers in the vector.

rows_to_delete <- c(
  281, 374, 653, 1185, 2198, 2233, 2234, 2235, 2236, 2237, 2402, 2491, 3686, 4157, 4605, 4606, 5489, 5936, 6239, 7843, 
  7844, 9865, 12384, 12385, 13058, 15079, 15164, 15884, 15921, 17594, 18918, 18938, 19503, 20586, 22313, 22658, 23622, 
  23723, 23724, 24748, 26621, 26622, 26632, 27077, 27078, 27921, 28413, 28833, 29392, 29579, 30032, 30976, 31975, 32072, 
  32563, 32843, 34577, 34808, 35681, 35919, 36026, 36113, 36936, 36957, 37197, 37905, 38000, 38357, 38867, 38965, 39464, 
  39906, 40518, 40646, 41358, 41958, 42026, 42514, 42669, 43026, 43462, 44481, 44716, 44757, 44813, 45213, 46201, 46349, 
  46463, 46563, 46861, 46961, 47116, 47370, 47982, 48122, 48932, 49300, 49666, 49938, 50913, 51127, 51375, 51430, 52048, 
  52083, 52354, 52451, 52973, 52974, 52975, 52976, 53657, 54294, 54394, 54395, 54512, 54574, 54575, 55338, 55339, 55340, 
  55341, 55342, 55343, 55344, 55345, 55346, 55347, 55348, 55349, 55350, 55411, 55457, 55459, 554
)

6 Remove specified rows from the data table

calo_occur <- calo_occ[-rows_to_delete, ]

7 Extract flagged rows by checking for NA values in the cleaned data

flagged_rows <- which(rowSums(is.na(calo_occur)) > 0)

8 Remove flagged rows from the original occurrence data

calochortus_occ <- calo_occur[-flagged_rows, ]

Show the filtered data

head(calochortus_occ)

9 Remove rows with specified names

calochortus_occ <- subset(calochortus_occ, !(scientificName %in% c("Calochortus acuminatus")))

10 Remove authorities from scientific names

calochortus_occ$scientificName <- gsub("^\\s*(\\w+\\s+\\w+).*", "\\1", calochortus_occ$scientificName) #removes everything after the species name
calochortus_occ <- subset(calochortus_occ, !grepl("Pursh", scientificName)) #Removed "Calochortus Pursh"

This next step will split genus and species names into two columns.

calochortus_occ$genus <- gsub("^(\\w+).*", "\\1", calochortus_occ$scientificName)
calochortus_occ$species <- gsub("^\\w+\\s+(\\w+)$", "\\1", calochortus_occ$scientificName)
calochortus_occ <- subset(calochortus_occ, select = -c(scientificName)) #deletes scientificName column
calochortus_occ <- calochortus_occ[, c("genus", "species", setdiff(names(calochortus_occ), c("genus", "species")))] #puts the genus and species as the first two columns

11 Show the modified data frame

head(calochortus_occ)

12 Save filtered occurrence data as a CSV file

write.csv(calochortus_occ, file = "calochortus_occ.csv", row.names = FALSE)