From 380b5a76589499cab8b8bdd4939ed32b5506b5c4 Mon Sep 17 00:00:00 2001 From: andybeet <22455149+andybeet@users.noreply.github.com> Date: Tue, 1 Oct 2024 10:17:55 -0400 Subject: [PATCH] collapse applyPropx arguments into a single argument. Make default = F --- R/aggregate_area.R | 22 +++++++--------------- R/get_comland_data.R | 13 ++++++++----- man/get_comland_data.Rd | 7 ++----- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/R/aggregate_area.R b/R/aggregate_area.R index bf21c56..86c44ee 100644 --- a/R/aggregate_area.R +++ b/R/aggregate_area.R @@ -4,27 +4,17 @@ #' statistical areas to a user defined area. Allows for species to be assigned by #' proportions to more than two user defined areas from one stat area #' +#'@inheritParams get_comland_data #'@param comland Data set generated by \code{get_comland_data} -#'@param userAreas Data frame. Definitions to aggregate statistical areas to user defined areas -#'@param areaDescription Character. Name of column in userAreas that defines the new area. -#'@param propDescription Character. Name of column in userAreas that defines the -#'proportions of landings assigned to new area. #' #'@noRd aggregate_area <- function(comData, userAreas, areaDescription, propDescription, - useForeign, channel, applyPropLand = T, - applyPropValue = T){ + useForeign, channel, applyProp){ #Pulling data message("Aggregating Areas ...") - #Add message - if(applyPropLand == F & applyPropValue == T){ - message("Can not apply proportions to Value and not Landings -- setting applyPropLand to F") - applyPropLand <- F - } - #Grab just the data comdata <- comData[[1]] @@ -81,8 +71,9 @@ aggregate_area <- function(comData, userAreas, areaDescription, propDescription, areas <- data.table::rbindlist(list(areas, div.prop), use.names = T) } + #Apply proportions to Landings #Merge new area descriptions to landings - if(applyPropLand){ + if(applyProp){ new.area <- merge(comdata, areas, by = c('NESPP3', 'AREA'), all.x = T, allow.cartesian = T) } else { @@ -98,10 +89,11 @@ aggregate_area <- function(comData, userAreas, areaDescription, propDescription, #Proportion landings to new areas new.area[, newspplivmt := SPPLIVMT * prop] - if(applyPropValue) new.area[, newsppvalue := SPPVALUE * prop] + # Apply proportions to Value #Drop extra columns and rename - if(applyPropValue){ + if(applyProp){ + new.area[, newsppvalue := SPPVALUE * prop] new.area[, c('AREA', 'SPPLIVMT', 'SPPVALUE', 'prop') := NULL] data.table::setnames(new.area, c('newarea', 'newspplivmt', 'newsppvalue'), c(areaDescription, 'SPPLIVMT', 'SPPVALUE')) diff --git a/R/get_comland_data.R b/R/get_comland_data.R index 8c7da04..79e6fa5 100644 --- a/R/get_comland_data.R +++ b/R/get_comland_data.R @@ -22,8 +22,7 @@ #'@param userAreas data frame. Spatial units in which Statistical areas should be aggregated (eg. \code{\link{mskeyAreas}}) #'@param areaDescription character string. Field name in \code{userAreas} denoting spatial unit. (Default = "EPU") #'@param propDescription character string. Field name in \code{userAreas} denoting the scaling factor. (Default = "MeanProp") -#'@param applyPropLand boolean. Apply the proportions in userAreas to the landings (Default = F) -#'@param applyPropValue boolean. Apply the proportions in userAreas to the value (Default = F) +#'@param applyProp boolean. Apply the proportions in userAreas to the landings and value (Default = T) #'@param aggGear boolean. Aggregate NEGEAR codes to larger "fleets" (Default = F) #'@param userGears data frame. Fleet designations in which NEGEAR codes should be grouped (eg. \code{\link{mskeyGears}}) #'@param fleetDescription character string. Field name in \code{userGears} denoting Fleet. (Default = "Fleet") @@ -58,7 +57,7 @@ get_comland_data <- function(channel, filterByYear = NA, refMonth = NA, disagSkatesHakes = T, aggArea = F, userAreas = comlandr::mskeyAreas, areaDescription = 'EPU', propDescription = 'MeanProp', - applyPropLand = T, applyPropValue = T, + applyProp = F, aggGear = F, userGears = comlandr::mskeyGears, fleetDescription = 'Fleet', unkVar = c('MONTH','NEGEAR','AREA'), @@ -112,6 +111,7 @@ get_comland_data <- function(channel, filterByYear = NA, channel, filterByYear, filterByArea) + saveRDS(comland,here::here("data_raw/data/64preagg.rds")) #Aggregate areas if(aggArea) comland <- aggregate_area(comland, userAreas, @@ -119,8 +119,11 @@ get_comland_data <- function(channel, filterByYear = NA, propDescription, useForeign, channel, - applyPropLand, - applyPropValue) + applyProp) + + saveRDS(comland,here::here("data-raw/data/64postagg.rds")) + + #Aggregate gears if(aggGear) comland <- aggregate_gear(comland, userGears, fleetDescription) diff --git a/man/get_comland_data.Rd b/man/get_comland_data.Rd index b338ce5..0b6c641 100644 --- a/man/get_comland_data.Rd +++ b/man/get_comland_data.Rd @@ -19,8 +19,7 @@ get_comland_data( userAreas = comlandr::mskeyAreas, areaDescription = "EPU", propDescription = "MeanProp", - applyPropLand = T, - applyPropValue = T, + applyProp = T, aggGear = F, userGears = comlandr::mskeyGears, fleetDescription = "Fleet", @@ -58,9 +57,7 @@ to communicate with the database engine. (see \code{\link[dbutils]{connect_to_da \item{propDescription}{character string. Field name in \code{userAreas} denoting the scaling factor. (Default = "MeanProp")} -\item{applyPropLand}{boolean. Apply the proportions in userAreas to the landings (Default = F)} - -\item{applyPropValue}{boolean. Apply the proportions in userAreas to the value (Default = F)} +\item{applyProp}{boolean. Apply the proportions in userAreas to the landings and value (Default = T)} \item{aggGear}{boolean. Aggregate NEGEAR codes to larger "fleets" (Default = F)}