Skip to content

Commit

Permalink
cleanup function
Browse files Browse the repository at this point in the history
  • Loading branch information
BenWilliams-NOAA committed Nov 14, 2023
1 parent 387b4bd commit 320d5c5
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions R/catch_to_sheet.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ catch_to_sheet <- function(year, data, area){

# calculate the total catches for most stocks
dat %>%
dplyr::filter(!(stock %in% c("pollock", "sablefish", "orox", "dsr")), !(grepl("GOA-wide", area))) %>%
dplyr::mutate(area = ifelse(area=='GOA-wide' & stock=="orox", 'W/C', area)) %>%
dplyr::filter(!(stock %in% c("pollock", "sablefish", "dsr")), !(grepl("GOA-wide", area))) %>%
dplyr::group_split(year, stock) %>%
purrr::map(~ tibble::add_row(.x,
area = "Total",
Expand Down Expand Up @@ -136,19 +137,32 @@ catch_to_sheet <- function(year, data, area){
year = yr,
catch = sum(.x$catch[.x$area!="EYAK/SEO"|.x$area!="PWS GHL"], na.rm = T))) -> poll

# orox and dsr catches, then rejoin with other catches
dat %>%
dplyr::filter(stock %in% c("orox", "dsr") | grepl("GOA-wide", area)) %>%
dplyr::mutate(area = ifelse(area!= "GOA-wide", "Total", area)) %>%
dplyr::group_by(stock, year, area) %>%
dplyr::summarise(catch = sum(catch), .groups="drop") %>%
bind_rows(tots, sable, poll) %>%
dplyr::filter(stock == "pop") %>%
dplyr::group_split(year, stock) %>%
purrr::map(~ tibble::add_row(.x,
area = "W/C/WYAK",
stock = tail(.x$stock, 1),
year = yr,
catch = sum(.x$catch[.x$area%in%c('W', 'C', "WYAK")], na.rm = T))) %>%
dplyr::bind_rows() %>%
dplyr::filter(area=="W/C/WYAK") -> pop

data.frame(stock="nork", year=yr, area='E') -> nork

# dsr catches, then rejoin with other catches
df %>%
dplyr::mutate(year = as.numeric(year)) %>%
dplyr::filter(!(year %in% yr)) %>%
bind_rows(dat %>%
dplyr::filter(stock %in% c("dsr") | grepl("GOA-wide", area), stock!='orox') %>%
dplyr::mutate(area = ifelse(area!= "GOA-wide", "Total", area)) %>%
dplyr::group_by(stock, year, area) %>%
dplyr::summarise(catch = sum(catch), .groups="drop") %>%
bind_rows(tots, sable, poll, pop, nork)) %>%
dplyr::mutate(stock = factor(stock, levels = goa_species$stock),
area = factor(area, levels = goa_levels)) %>%
dplyr::arrange(stock, area, year) %>% dplyr::select(-catch) %>%
dplyr::left_join(df %>%
dplyr::mutate(year = as.numeric(year)), .,
by = c('stock', 'year', 'area')) %>%
dplyr::arrange(stock, year, area) %>%
dplyr::select(catch) -> ct


Expand Down

0 comments on commit 320d5c5

Please sign in to comment.