### Install CoordinateCleaner Source: https://docs.ropensci.org/CoordinateCleaner/index.html Installation instructions for stable and developmental versions. ```R install.packages("CoordinateCleaner") library(CoordinateCleaner) ``` ```R devtools::install_github("ropensci/CoordinateCleaner") library(CoordinateCleaner) ``` -------------------------------- ### Install CoordinateCleaner from GitHub Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_GBIF_data_with_CoordinateCleaner.html Installs the development version of the package using devtools. ```R install.packages("devtools") library(devtools) install_github("ropensci/CoordinateCleaner") ``` -------------------------------- ### Example usage of write_pyrate Source: https://docs.ropensci.org/CoordinateCleaner/reference/write_pyrate.html Demonstrates how to prepare a data frame and status vector before calling write_pyrate. ```R minages <- runif(250, 0, 65) exmpl <- data.frame(accepted_name = sample(letters, size = 250, replace = TRUE), lng = runif(250, min = 42, max = 51), lat = runif(250, min = -26, max = -11), min_ma = minages, max_ma = minages + runif(250, 0.1, 65)) #a vector with the status for each record, #make sure species are only classified as either extinct or extant, #otherwise the function will drop an error status <- sample(c("extinct", "extant"), size = nrow(exmpl), replace = TRUE) #or from a list of species status <- sample(c("extinct", "extant"), size = length(letters), replace = TRUE) names(status) <- letters status <- status[exmpl$accepted_name] if (FALSE) { # \dontrun{ write_pyrate(x = exmpl,fname = "test", status = status) } # } ``` -------------------------------- ### cf_equal example usage Source: https://docs.ropensci.org/CoordinateCleaner/reference/cf_equal.html Example demonstrating how to flag records with equal minimum and maximum ages in a data frame. ```R minages <- runif(n = 10, min = 0.1, max = 25) x <- data.frame(species = letters[1:10], min_ma = minages, max_ma = minages + runif(n = 10, min = 0, max = 10)) x <- rbind(x, data.frame(species = "z", min_ma = 5, max_ma = 5)) cf_equal(x, value = "flagged") #> Testing age validity #> Flagged 1 records. #> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE FALSE ``` -------------------------------- ### Example usage of cc_iucn Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_iucn.html Demonstrates setting up dummy species data and range polygons to test the cc_iucn function. ```R library(terra) #> terra 1.9.34 x <- data.frame(species = c("A", "B"), decimalLongitude = runif(100, -170, 170), decimalLatitude = runif(100, -80,80)) range_species_A <- cbind(c(-45,-45,-60,-60,-45), c(-10,-25,-25,-10,-10)) rangeA <- terra::vect(range_species_A, "polygons") range_species_B <- cbind(c(15,15,32,32,15), c(10,-10,-10,10,10)) rangeB <- terra::vect(range_species_B, "polygons") range <- terra::vect(list(rangeA, rangeB)) range$binomial <- c("A", "B") cc_iucn(x = x, range = range, buffer = 0) #> Testing natural ranges #> Warning: no projection information for reference found, #> assuming '+proj=longlat +datum=WGS84 +no_defs' #> Removed 100 records. #> [1] species decimalLongitude decimalLatitude #> <0 rows> (or 0-length row.names) ``` -------------------------------- ### cc_gbif Examples Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_gbif.html Demonstrates how to use cc_gbif to clean a data frame or return a logical vector of flagged records. ```R x <- data.frame(species = "A", decimalLongitude = c(12.58, 12.58), decimalLatitude = c(55.67, 30.00)) cc_gbif(x) #> Testing GBIF headquarters, flagging records around Copenhagen #> Removed 1 records. #> species decimalLongitude decimalLatitude #> 2 A 12.58 30 cc_gbif(x, value = "flagged") #> Testing GBIF headquarters, flagging records around Copenhagen #> Flagged 1 records. #> [1] FALSE TRUE ``` -------------------------------- ### Example usage of cc_zero Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_zero.html Demonstrates cleaning a data frame and returning a logical vector of flagged records. ```R x <- data.frame(species = "A", decimalLongitude = c(0,34.84, 0, 33.98), decimalLatitude = c(23.08, 0, 0, 15.98)) cc_zero(x) #> Testing zero coordinates #> Warning: [vect] guessed crs #> Removed 3 records. #> species decimalLongitude decimalLatitude #> 4 A 33.98 15.98 cc_zero(x, value = "flagged") #> Testing zero coordinates #> Warning: [vect] guessed crs #> Flagged 3 records. #> [1] FALSE FALSE FALSE TRUE ``` -------------------------------- ### Flag records near biodiversity institutions Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_inst.html Example demonstrating how to flag records near institutions using a data frame and specific buffer settings. ```R x <- data.frame(species = letters[1:10], decimalLongitude = c(runif(99, -180, 180), 37.577800), decimalLatitude = c(runif(99, -90,90), 55.710800)) #large buffer for demonstration, using geod = FALSE for shorter runtime cc_inst(x, value = "flagged", buffer = 10, geod = FALSE) #> Testing biodiversity institutions #> Flagged 1 records. #> [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [13] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [25] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [37] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [49] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [61] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [73] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [85] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE #> [97] TRUE TRUE TRUE FALSE if (FALSE) { # \dontrun{ #' cc_inst(x, value = "flagged", buffer = 50000) #geod = T } # } ``` -------------------------------- ### Example of plotting spatialvalid results Source: https://docs.ropensci.org/CoordinateCleaner/reference/plot.spatialvalid.html Demonstrates creating a spatialvalid object using clean_coordinates and visualizing the results with the plot method. ```R exmpl <- data.frame(species = sample(letters, size = 250, replace = TRUE), decimalLongitude = runif(250, min = 42, max = 51), decimalLatitude = runif(250, min = -26, max = -11)) test <- clean_coordinates(exmpl, species = "species", tests = c("sea", "gbif", "zeros"), verbose = FALSE) #> Warning: [vect] guessed crs summary(test) #> .val .zer .gbf .summary #> 0 0 0 0 plot(test) #> Warning: `aes_string()` was deprecated in ggplot2 3.0.0. #> ℹ Please use tidy evaluation idioms with `aes()`. #> ℹ See also `vignette("ggplot2-in-packages")` for more information. #> ℹ The deprecated feature was likely used in the CoordinateCleaner package. #> Please report the issue at #> . ``` -------------------------------- ### Detect and Clean Duplicate Records Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_dupl.html Examples demonstrating how to flag duplicate records as a logical vector or return a cleaned data frame with additional columns for duplicate identification. ```R x <- data.frame(species = letters[1:10], decimalLongitude = sample(x = 0:10, size = 100, replace = TRUE), decimalLatitude = sample(x = 0:10, size = 100, replace = TRUE), collector = "Bonpl", collector.number = c(1001, 354), collection = rep(c("K", "WAG","FR", "P", "S"), 20)) cc_dupl(x, value = "flagged") ``` ```R cc_dupl(x, additions = c("collector", "collector.number")) ``` -------------------------------- ### Identify temporal range outliers Source: https://docs.ropensci.org/CoordinateCleaner/reference/cf_range.html Example demonstrating the use of cf_range to flag temporal outliers in a generated dataset. ```R minages <- runif(n = 11, min = 0.1, max = 25) x <- data.frame(species = c(letters[1:10], "z"), lng = c(runif(n = 9, min = 4, max = 16), 75, 7), lat = c(runif(n = 11, min = -5, max = 5)), min_ma = minages, max_ma = minages + c(runif(n = 10, min = 0, max = 5), 25)) cf_range(x, value = "flagged", taxon = "") ``` -------------------------------- ### Identify temporal outliers in fossil data Source: https://docs.ropensci.org/CoordinateCleaner/reference/cf_age.html Example usage of cf_age to flag temporal outliers in a sample dataset. ```R minages <- c(runif(n = 11, min = 10, max = 25), 62.5) x <- data.frame(species = c(letters[1:10], rep("z", 2)), min_ma = minages, max_ma = c(minages[1:11] + runif(n = 11, min = 0, max = 5), 65)) cf_age(x, value = "flagged", taxon = "") ``` -------------------------------- ### Run cc_cap on sample data Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_cap.html Example usage of cc_cap to filter a data frame of coordinates, returning either the cleaned data or a logical flag vector. ```R if (FALSE) { # \dontrun{ x <- data.frame(species = letters[1:10], decimalLongitude = c(runif(99, -180, 180), -47.882778), decimalLatitude = c(runif(99, -90, 90), -15.793889)) cc_cap(x) cc_cap(x, value = "flagged") } # } ``` -------------------------------- ### Validate Coordinates with cc_val Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_val.html This example demonstrates how to use cc_val to clean a data frame containing invalid coordinate entries. ```R x <- data.frame(species = letters[1:10], decimalLongitude = c(runif(106, -180, 180), NA, "13W33'", "67,09", 305), decimalLatitude = runif(110, -90,90)) cc_val(x) ``` -------------------------------- ### Detect geographic outliers with cc_outl Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_outl.html This example demonstrates how to use cc_outl on a data frame containing species names and coordinates. The function returns the input data frame with flagged records. ```R x <- data.frame(species = letters[1:10], decimalLongitude = runif(100, -180, 180), decimalLatitude = runif(100, -90,90)) cc_outl(x) #> Testing geographic outliers #> Removed 0 records. #> species decimalLongitude decimalLatitude #> 1 a 141.907373 41.7730578 #> 2 b -100.689707 -16.3607806 #> 3 c -75.025282 23.5544050 #> 4 d 112.643550 -78.2199030 #> 5 e -164.303161 -63.2354127 #> 6 f 1.897799 -39.6536823 #> 7 g -93.003883 63.1182257 #> 8 h 32.091271 -86.1226595 #> 9 i 169.610605 -44.4906216 #> 10 j -96.220439 28.2080702 #> 11 a 132.270486 52.0468937 #> 12 b -140.125773 37.5922166 #> 13 c 108.537041 -78.3732614 #> 14 d 56.852804 2.0360419 #> 15 e 54.739547 -53.8328970 #> 16 f -98.523848 56.5408067 #> 17 g 127.432667 27.6804137 #> 18 h -23.181466 -26.4146432 #> 19 i -75.858575 17.3870813 #> 20 j 49.530481 40.9867872 #> 21 a -84.765795 89.2023371 #> 22 b -40.943384 -0.3500511 #> 23 c 42.909829 -48.5469415 #> 24 d 15.004110 -35.7742237 #> 25 e -5.460840 39.6840018 #> 26 f 95.146171 -42.9470247 #> 27 g 165.969697 -70.6146573 #> 28 h -82.589568 -42.7639118 #> 29 i 83.016080 -56.3928870 #> 30 j -131.700403 7.6395023 #> 31 a -162.576638 -78.2066746 #> 32 b 73.298545 68.4693246 #> 33 c -127.003034 -39.7417433 #> 34 d 96.895920 31.0866609 #> 35 e -116.513960 -79.1764427 #> 36 f -140.126760 87.9273990 #> 37 g 157.803267 -39.1904224 #> 38 h 124.729610 9.7402260 #> 39 i 25.611689 70.2151208 #> 40 j 64.473626 -85.8956301 #> 41 a -147.843992 31.9839217 #> 42 b 102.734685 -29.3292366 #> 43 c -98.295319 -64.5847030 #> 44 d -18.655753 -26.5384822 #> 45 e -121.958051 -17.7399258 #> 46 f -116.599792 70.0045834 #> 47 g -108.637876 -33.7294462 #> 48 h -51.259927 -78.9021525 #> 49 i -114.720047 32.3075275 #> 50 j 22.012150 -21.9307376 #> 51 a 57.269883 15.9066627 #> 52 b 57.691227 -1.4438637 #> 53 c -179.135716 -38.5601501 #> 54 d 177.640572 -88.4844729 #> 55 e 45.896123 9.1393735 #> 56 f -174.733079 56.3815634 #> 57 g -106.135842 25.8434254 #> 58 h 58.707599 -77.5688395 #> 59 i -13.052149 41.1689289 #> 60 j -50.274663 30.9800157 #> 61 a 68.171901 57.5641784 #> 62 b -86.810116 74.6318804 #> 63 c 126.433812 34.1947225 #> 64 d -7.198311 -1.4439955 #> 65 e 13.415900 26.4471571 #> 66 f 58.190363 -7.8273078 #> 67 g -71.692376 -67.4196081 #> 68 h -85.139454 18.7856953 #> 69 i -70.183393 58.8288125 #> 70 j 134.984976 21.4880852 #> 71 a 78.637398 61.3896679 #> 72 b -33.508910 -13.0956106 #> 73 c -173.030590 48.4716067 #> 74 d -161.935071 38.1853424 #> 75 e 72.810372 -78.5939025 #> 76 f -156.106703 -13.3190575 #> 77 g -166.992347 -74.1100242 #> 78 h -172.744670 -52.5029725 #> 79 i -45.798805 -48.9130428 #> 80 j -98.760455 -80.9204141 #> 81 a -2.909484 11.1278254 #> 82 b -19.207744 34.9643060 #> 83 c 68.473764 -48.9703222 #> 84 d 123.386393 82.8564489 #> 85 e -44.699880 -1.2893056 #> 86 f 174.560288 40.1775634 #> 87 g 162.873479 -1.5546451 #> 88 h 176.363463 62.1639146 #> 89 i -20.473792 -85.4802606 #> 90 j -124.645013 8.7120437 #> 91 a 160.101836 81.9818670 #> 92 b 7.007909 -64.7096640 ``` -------------------------------- ### Identify Artificial Hotspot Records Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_aohi.html Example usage of cc_aohi to filter a data frame of species occurrences. ```R x <- data.frame(species = letters[1:10], decimalLongitude = c(runif(99, -180, 180), -47.92), decimalLatitude = c(runif(99, -90,90), -15.78)) cc_aohi(x) ``` -------------------------------- ### Prepare and export PBDB data for PyRate Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_PBDB_fossils_with_CoordinateCleaner.html Formats taxon names, generates mock status data, joins the datasets, and writes the final output for PyRate. ```R # replace blanks in taxon names cl$taxon_name <- gsub("[[:blank:]]{1,}","_", cl$taxon_name) #simulated current status, soley for demonstration purposes, replace with your own data mock_status <- data.frame(taxon_name = unique(cl$taxon_name), status = sample(c("extinct", "extant"), size = length(unique(cl$taxon_name)), replace = TRUE)) #add current status to fossils cl2 <- inner_join(cl, mock_status, by = "taxon_name") #Write PyRate input to disk write_pyrate(cl, fname = "paleobioDB_angiosperms", status = cl2$status, taxon = "taxon_name", min_age = "late_age", max_age = "early_age") ``` -------------------------------- ### Flagging records with default gazetteer Source: https://docs.ropensci.org/CoordinateCleaner/articles/Using_custom_gazetteers.html Demonstrates downloading GBIF data and using the default cc_sea gazetteer, which may be too restrictive for coastal species. ```R library(CoordinateCleaner) library(dplyr) library(ggplot2) library(rgbif) library(viridis) library(terra) #download data from GBIF dat <- rgbif::occ_search(scientificName = "Avicennia", limit = 1000, hasCoordinate = T) dat <- dat$data dat <- dat %>% dplyr::select(species = name, decimalLongitude = decimalLongitude, decimalLatitude = decimalLatitude, countryCode) # run with default gazetteer outl <- cc_sea(dat, value = "flagged") ## OGR data source with driver: ESRI Shapefile ## Source: "C:\Users\az64mycy\AppData\Local\Temp\Rtmp4SRhHV", layer: "ne_110m_land" ## with 127 features ## It has 3 fields plo <- data.frame(dat, outlier = as.factor(!outl)) #plot results ggplot() + borders(fill = "grey60") + geom_point(data = plo, aes(x = decimalLongitude, y = decimalLatitude, col = outlier)) + scale_color_viridis(discrete = T, name = "Flagged outlier") + coord_fixed() + theme_bw() + theme(legend.position = "bottom") ``` -------------------------------- ### Load and prepare PBDB test dataset Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_PBDB_fossils_with_CoordinateCleaner.html Download fossil occurrence data from the Paleobiology Database and ensure coordinate and age columns are numeric. ```R #load data dat <- paleobioDB::pbdb_occurrences(base_name = "Magnoliopsida", vocab = "pbdb", limit = 5000, show = c("coords", "phylo", "attr", "loc", "time", "rem")) dat <- dat %>% mutate(lng = as.numeric(lng), lat = as.numeric(lat), early_age = as.numeric(early_age), late_age = as.numeric(late_age)) rownames(dat) <- NULL ``` -------------------------------- ### write_pyrate(x, status, fname, ...) Source: https://docs.ropensci.org/CoordinateCleaner/reference/write_pyrate.html Creates the input necessary to run PyRate, based on a data.frame with fossil ages and a vector of the extinction status for each sample. ```APIDOC ## write_pyrate(x, status, fname, taxon = "accepted_name", min_age = "min_ma", max_age = "max_ma", trait = NULL, path = getwd(), replicates = 1, cutoff = NULL, random = TRUE) ### Description Creates the input necessary to run PyRate, based on a data.frame with fossil ages (as derived e.g. from clean_fossils) and a vector of the extinction status for each sample. Creates files in the working directory. ### Parameters - **x** (data.frame) - Required - Containing fossil records with taxon names, ages, and geographic coordinates. - **status** (vector) - Required - A vector of character strings of length nrow(x) indicating for each record “extinct” or “extant”. - **fname** (character) - Required - The prefix to use for the output files. - **taxon** (character) - Optional - The column with the taxon name. Default = “accepted_name”. - **min_age** (character) - Optional - The column with the minimum age. Default = “min_ma”. - **max_age** (character) - Optional - The column with the maximum age. Default = “max_ma”. - **trait** (numeric vector) - Optional - Indicating trait values for each record. Default = NULL. - **path** (character) - Optional - The absolute path to write the output files. Default is the working directory. - **replicates** (numeric) - Optional - The number of replicates for the randomized age generation. Default = 1. - **cutoff** (numeric) - Optional - Specify a threshold to exclude fossil occurrences with a high temporal uncertainty. - **random** (logical) - Optional - Specify whether to take a random age (between MinT and MaxT) for each occurrence or the midpoint age. Default = TRUE. ### Response - **Output** (files) - PyRate input files generated in the specified path. ``` -------------------------------- ### Simulate and Test Rasterized Data Source: https://docs.ropensci.org/CoordinateCleaner/reference/cd_round.html Demonstrates how to simulate a dataset with a biased grid and run the cd_round test on it. ```R #simulate bias grid, one degree resolution, 10% error on a 1000 records dataset #simulate biased fraction of the data, grid resolution = 1 degree #simulate non-biased fraction of the data bi <- sample(3 + 0:5, size = 100, replace = TRUE) mu <- runif(3, 0, 15) sig <- runif(3, 0.1, 5) cl <- rnorm(n = 900, mean = mu, sd = sig) lon <- c(cl, bi) bi <- sample(9:13, size = 100, replace = TRUE) mu <- runif(3, 0, 15) sig <- runif(3, 0.1, 5) cl <- rnorm(n = 900, mean = mu, sd = sig) lat <- c(cl, bi) #add biased data inp <- data.frame(decimalLongitude = lon, decimalLatitude = lat, dataset = "test") #run test if (FALSE) { # \dontrun{ cd_round(inp, value = "dataset") } # } ``` -------------------------------- ### Flagging non-terrestrial records with cc_coun Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_coun.html Example usage of cc_coun to flag records that do not match the provided country code. ```R if (FALSE) { # \dontrun{ x <- data.frame(species = letters[1:10], decimalLongitude = runif(100, -20, 30), decimalLatitude = runif(100, 35,60), countrycode = "RUS") cc_coun(x, value = "flagged")#non-terrestrial records are flagged as wrong. } # } ``` -------------------------------- ### Loading and visualizing a custom buffered gazetteer Source: https://docs.ropensci.org/CoordinateCleaner/articles/Using_custom_gazetteers.html Loads the built-in buffered land reference for use as a custom gazetteer. ```R # The buffered custom gazetteer data("buffland") buffland <- terra::vect(buffland) plot(buffland) ``` -------------------------------- ### Flag records near country centroids Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_cen.html Example usage of cc_cen to test a data frame of species coordinates for proximity to country centroids. ```R x <- data.frame(species = letters[1:10], decimalLongitude = c(runif(99, -180, 180), -47.92), decimalLatitude = c(runif(99, -90,90), -15.78)) cc_cen(x, geod = FALSE) #> Testing country centroids #> Removed 0 records. #> species decimalLongitude decimalLatitude #> 1 a -41.986559 -67.231328 #> 2 b 179.874885 78.719439 #> 3 c -54.252343 -51.051559 #> 4 d 161.034576 29.896562 #> 5 e -102.204009 -53.297878 #> 6 f -168.446625 8.746459 #> 7 g -127.686296 58.950255 #> 8 h 127.578200 -67.039623 #> 9 i -103.266248 -42.986499 #> 10 j -104.288134 -38.084935 #> 11 a -165.772552 -87.341577 #> 12 b 160.118929 63.670271 #> 13 c -91.825925 -15.471391 #> 14 d 101.204125 45.182492 #> 15 e -76.234619 39.422512 #> 16 f 135.128848 2.763954 #> 17 g -73.529966 85.470526 #> 18 h 174.069147 -25.060039 #> 19 i 32.341520 -59.328809 #> 20 j 93.297017 50.965321 #> 21 a 120.987110 -84.433699 #> 22 b 94.615009 52.095665 #> 23 c -29.782827 -60.011338 #> 24 d -130.293057 -84.831738 #> 25 e -150.895814 51.471210 #> 26 f 56.153745 58.555415 #> 27 g 36.721389 83.743275 #> 28 h 56.518499 -21.844588 #> 29 i -61.445821 -58.610461 #> 30 j 172.610719 18.675072 #> 31 a 77.467008 54.983472 #> 32 b 134.146910 -83.397706 #> 33 c 173.982149 41.923920 #> 34 d -101.317323 -51.250922 #> 35 e 59.230823 -87.119434 #> 36 f -39.756946 -66.851536 #> 37 g -163.417090 33.491359 #> 38 h 42.089241 25.549428 #> 39 i 35.450998 -31.079066 #> 40 j -33.532693 -20.253243 #> 41 a 128.998134 37.350071 #> 42 b 6.365227 35.577977 #> 43 c 172.545628 76.618218 #> 44 d -173.874352 -7.376810 #> 45 e 62.441217 17.271756 #> 46 f -46.342842 -60.298022 #> 47 g 150.483829 12.043761 #> 48 h 64.072114 71.653927 #> 49 i 59.454886 17.005011 #> 50 j 92.174792 59.704185 #> 51 a 15.421376 16.813506 #> 52 b -93.856285 50.214714 #> 53 c 3.201686 -18.401104 #> 54 d -29.784826 62.978899 #> 55 e 81.701587 43.532205 #> 56 f 49.566799 -32.797761 #> 57 g -37.292416 -69.897560 #> 58 h 165.413739 -71.802834 #> 59 i -72.483109 54.017595 #> 60 j -161.927580 -21.609586 #> 61 a 27.427471 -80.513825 #> 62 b -101.553909 87.571566 #> 63 c -134.691743 18.751314 #> 64 d 157.734967 -63.219110 #> 65 e 108.459046 6.976831 #> 66 f 92.899303 -67.357939 #> 67 g 11.723459 83.372716 #> 68 h 16.849719 -81.551697 #> 69 i -145.466459 -60.946347 #> 70 j -40.194089 78.567737 #> 71 a -117.953320 84.722645 #> 72 b 68.661305 37.849329 #> 73 c 63.075061 69.316367 #> 74 d 160.666145 85.837557 #> 75 e -109.360972 -83.718064 #> 76 f 168.709501 -11.023692 #> 77 g -40.645340 26.004557 #> 78 h 54.123804 89.069833 #> 79 i 113.254633 -33.360728 #> 80 j -154.452682 64.058754 #> 81 a 9.658917 7.260783 #> 82 b 94.850937 67.215123 #> 83 c -23.260809 2.805412 #> 84 d 18.890041 66.502674 #> 85 e -106.548967 64.174067 #> 86 f -168.830634 -27.777027 ``` -------------------------------- ### Load required libraries Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_GBIF_data_with_CoordinateCleaner.html Loads the necessary R packages for data manipulation, visualization, and cleaning. ```R library(countrycode) library(CoordinateCleaner) library(dplyr) library(ggplot2) library(rgbif) library(sf) ``` -------------------------------- ### Clean coordinates with basic and advanced tests Source: https://docs.ropensci.org/CoordinateCleaner/reference/clean_coordinates.html Demonstrates basic coordinate validation and how to execute a comprehensive suite of tests including capitals, centroids, and outliers. ```R exmpl <- data.frame(species = sample(letters, size = 250, replace = TRUE), decimalLongitude = runif(250, min = 42, max = 51), decimalLatitude = runif(250, min = -26, max = -11)) test <- clean_coordinates(x = exmpl, tests = c("equal")) #> Testing coordinate validity #> Flagged 0 records. #> Testing equal lat/lon #> Flagged 0 records. #> Flagged 0 of 250 records, EQ = 0. if (FALSE) { # \dontrun{ #run more tests test <- clean_coordinates(x = exmpl, tests = c("capitals", "centroids","equal", "gbif", "institutions", "outliers", "seas", "zeros")) } # } summary(test) #> .val .equ .summary #> 0 0 0 ``` -------------------------------- ### Load and inspect the institutions dataset Source: https://docs.ropensci.org/CoordinateCleaner/reference/institutions.html Load the institutions data frame and display its structure to understand the available variables and data types. ```R data(institutions) str(institutions) ``` -------------------------------- ### Execute cc_urb on sample data Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_urb.html Demonstrates running the function on a data frame to return either cleaned records or a logical vector of flags. ```R if (FALSE) { # \dontrun{ x <- data.frame(species = letters[1:10], decimalLongitude = runif(100, -180, 180), decimalLatitude = runif(100, -90,90)) cc_urb(x) cc_urb(x, value = "flagged") } # } ``` -------------------------------- ### Summarize and visualize cleaning flags Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_GBIF_data_with_CoordinateCleaner.html Display a summary of flagged records and visualize their spatial distribution. ```R summary(flags) plot(flags, lon = "decimalLongitude", lat = "decimalLatitude") ``` -------------------------------- ### Load buffland dataset in R Source: https://docs.ropensci.org/CoordinateCleaner/reference/buffland.html Loads the global coastline buffer dataset for use with the cc_sea function. ```R data("buffland") ``` -------------------------------- ### Obtain and prepare GBIF data Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_GBIF_data_with_CoordinateCleaner.html Downloads occurrence data for Panthera leo from GBIF and filters for relevant columns and valid coordinates. ```R #obtain data from GBIF via rgbif dat <- occ_search(scientificName = "Panthera leo", limit = 5000, hasCoordinate = TRUE) dat <- dat$data # names(dat) # a lot of columns # select columns of interest dat <- dat %>% dplyr::select(species, decimalLongitude, decimalLatitude, countryCode, individualCount, gbifID, family, taxonRank, coordinateUncertaintyInMeters, year, basisOfRecord, institutionCode, datasetName) # remove records without coordinates dat <- dat %>% filter(!is.na(decimalLongitude)) %>% filter(!is.na(decimalLatitude)) ``` -------------------------------- ### Visualize fossil records on a map Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_PBDB_fossils_with_CoordinateCleaner.html Create a world map plot to visualize the spatial distribution of the fossil occurrences. ```R #plot data to get an overview wm <- borders("world", colour = "gray50", fill = "gray50") dat %>% ggplot(aes(x = lng, y = lat)) + coord_fixed() + wm + geom_point( colour = "darkred", size = 0.5) + theme_bw() ``` -------------------------------- ### clean_dataset(x, lon, lat, ds, tests, value, verbose, ...) Source: https://docs.ropensci.org/CoordinateCleaner/reference/clean_dataset.html Tests for problems associated with coordinate conversions and rounding, based on dataset properties. ```APIDOC ## clean_dataset ### Description Tests for problems associated with coordinate conversions and rounding, based on dataset properties. Includes tests to identify contributing datasets with potential errors with converting ddmm to dd.dd, and periodicity in the data decimals indicating rounding or a raster basis. ### Usage clean_dataset(x, lon = "decimalLongitude", lat = "decimalLatitude", ds = "dataset", tests = c("ddmm", "periodicity"), value = "dataset", verbose = TRUE, ...) ### Arguments - **x** (data.frame) - Required - Containing geographical coordinates and species names. - **lon** (character) - Optional - The column with the longitude coordinates. Default = "decimalLongitude". - **lat** (character) - Optional - The column with the latitude coordinates. Default = "decimalLatitude". - **ds** (character) - Optional - The column with the dataset of each record. Default = "dataset". - **tests** (vector) - Optional - A vector of character strings indicating which tests to run. Default = c("ddmm", "periodicity"). - **value** (character) - Optional - Defining the output value. Default = "dataset". - **verbose** (logical) - Optional - If TRUE reports the name of the test and the number of records flagged. - **...** (dots) - Optional - Additional arguments to be passed to cd_ddmm and cd_round to customize test sensitivity. ``` -------------------------------- ### Load AHOI dataset in R Source: https://docs.ropensci.org/CoordinateCleaner/reference/aohi.html Loads the AHOI data frame into the R environment for use. ```R data("aohi") ``` -------------------------------- ### Load and inspect countryref data Source: https://docs.ropensci.org/CoordinateCleaner/reference/countryref.html Load the countryref dataset into the R environment and display the first few rows to understand its structure. ```R data(countryref) head(countryref) ``` -------------------------------- ### Inspect minimum ages Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_PBDB_fossils_with_CoordinateCleaner.html Displays the tail of the table containing minimum age values for the cleaned dataset. ```R #minimum ages tail(table(cl$late_age)) ``` -------------------------------- ### Visualize Early Age Distribution Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_PBDB_fossils_with_CoordinateCleaner.html Displays a histogram of the early_age column from the dataset. ```R ggplot(cl) + geom_histogram(aes(x = early_age)) ``` -------------------------------- ### Usage of write_pyrate Source: https://docs.ropensci.org/CoordinateCleaner/reference/write_pyrate.html Defines the function signature and parameters for generating PyRate input files. ```R write_pyrate( x, status, fname, taxon = "accepted_name", min_age = "min_ma", max_age = "max_ma", trait = NULL, path = getwd(), replicates = 1, cutoff = NULL, random = TRUE ) ``` -------------------------------- ### Clean fossil data using clean_fossils wrapper Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_PBDB_fossils_with_CoordinateCleaner.html Uses the clean_fossils wrapper function to perform automated quality tests on fossil data and separate valid from flagged records. ```R #run automated testing flags <- clean_fossils(x = dat, lat = "lat", lon = "lng", taxon = "taxon_name", min_age = "late_age", max_age = "early_age", value = "spatialvalid") head(flags) cl <- dat[flags$.summary,] #the cleaned records fl_rec <- dat[!flags$.summary,] # the flagged records for verification ``` -------------------------------- ### Validate country identity with ISO3 conversion Source: https://docs.ropensci.org/CoordinateCleaner/articles/Cleaning_PBDB_fossils_with_CoordinateCleaner.html Converts country codes to ISO3 format and tests if coordinates match the assigned country, handling potential mismatches like historical political boundaries. ```R #adapt country code to ISO3, for country test cs_ma <- "GBR" names(cs_ma) <- "UK" cl$cc_iso3 <- countrycode(cl$cc, origin = "iso2c", destination = "iso3c", custom_match = cs_ma) cl <- cc_coun(cl, lat = "lat", lon = "lng", iso3 = "cc_iso3") ``` -------------------------------- ### Identify geographic outliers using quantile method Source: https://docs.ropensci.org/CoordinateCleaner/reference/cc_outl.html Uses the quantile method to flag geographic outliers in the dataset x. ```R cc_outl(x, method = "quantile", value = "flagged") ``` -------------------------------- ### Identify spatio-temporal outliers with cf_outl Source: https://docs.ropensci.org/CoordinateCleaner/reference/cf_outl.html Demonstrates the usage of cf_outl on a sample data frame containing species, coordinates, and age ranges. ```R minages <- c(runif(n = 11, min = 10, max = 25), 62.5) x <- data.frame(species = c(letters[1:10], rep("z", 2)), lng = c(runif(n = 10, min = 4, max = 16), 75, 7), lat = c(runif(n = 12, min = -5, max = 5)), min_ma = minages, max_ma = c(minages[1:11] + runif(n = 11, min = 0, max = 5), 65)) cf_outl(x, value = "flagged", taxon = "") ```