From d4938bc90452f6e70a150b46ed1d8776ff29913d Mon Sep 17 00:00:00 2001 From: Chantel Wetzel - NOAA Federal Date: Tue, 19 Nov 2024 09:28:44 -0800 Subject: [PATCH] improve data checks --- R/get_biological_data.R | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/R/get_biological_data.R b/R/get_biological_data.R index bbcfa0a..38706c1 100644 --- a/R/get_biological_data.R +++ b/R/get_biological_data.R @@ -33,6 +33,21 @@ get_biological_data <- function( if (length(gear_names) != length(gear_groups)) { cli::cli_abort("The gear groups and names are not of the same length.") } + if (any(!"LENGTH" %in% colnames(data))) { + cli::cli_abort("The LENGTH/length column is not present in the data.") + } + if (any(!"AGE" %in% colnames(data))) { + cli::cli_abort("The AGE/age column is not present in the data.") + } + present_data <- data |> + dplyr::filter(species == species_name) |> + dplyr::summarise( + do_lengths = sum(!is.na(LENGTH)), + do_ages = sum(!is.na(AGE)) + ) + if (sum(present_data) == 0) { + cli::cli_abort("There are no length or age samples in the data for {species_name}.") + } # Remove duplicate columns data <- data |> @@ -75,8 +90,12 @@ get_biological_data <- function( ci_not_met <- ci_check |> dplyr::filter(n_vessels < 3, ) if (dim(ci_not_met)[1] > 0) { remove <- NULL - for (f in unique(ci_not_met[, "fleet"])) { - remove <- c(remove, which(data$fleet == f & data$year %in% ci_not_met[ci_not_met$fleet == f, "year"])) + for (f in 1:dim(ci_not_met)[1]) { + remove <- c(remove, + which( + data[, "fleet"] == ci_not_met[f, "fleet"] & + data[, "year"] == ci_not_met[f, "year"] & + data[, "catch_shares"] == ci_not_met[f, "catch_shares"] )) } data <- data[-remove, ] cli::cli_inform(