This report is automatically generated with the R
package knitr
(version 1.40
)
.
#Notes from TMDL Stafff Report Section 4.7.1 # Only fish fillet data were used in the human and eagle trophic level food group analysis. Humans typically consume fish fillets, while wildlife species, including eagles, eat whole fish. However, all the data for large fish typically consumed by eagles and other large wildlife species are from fillet samples, making it necessary to use fillet information for these species.24 Whole fish data were used for the smaller wildlife species food groups. #The below code is used to test above rationale by filtering for 'Fillet' or 'Whole Body' below, to see if this rationale severely limits the number of Subareas included in the TLG analysis. TLG_CompositeWeighted_AvgHg_TissueOptns <- function(dataframe, ...){ ## Calculate Avg [Hg] (weighted by composite size) for each TLG by Subarea dataframe <- dataframe %>% ungroup # ungroup dataframe as precaution so filtering works as expected # grouping_var <- quo(Subarea) - to test function line-wise grouping_var <- enquos(...) #TL4 Fish (150-500 mm) tl4_500 <- dataframe %>% mutate(TLG = case_when(TrophicLevel =='TL4' & TLAvgLength >=150 & TLAvgLength <=500 ~ 'TL4 Fish (150-500 mm)', T ~ NA_character_)) %>% filter(!is.na(TLG)) both <- tl4_500 %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = 'Fillet & Whole Body', .groups='drop') alt <- tl4_500 %>% filter(TissueName == 'Fillet') if(nrow(alt) > 0){ alt <- alt %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = TissueName[1], .groups='drop') } else{ alt <- both %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = NA, CompositeSamples = NA, CompositeTotalFish = NA, Years = NA, Tissue = 'Fillet', .groups='drop') } tl4_500_results <- rbind(both, alt) #TL4 Fish (150-350 mm) tl4_350 <- dataframe %>% mutate(TLG = case_when(TrophicLevel =='TL4' & TLAvgLength >=150 & TLAvgLength <=350 ~ 'TL4 Fish (150-350 mm)', T ~ NA_character_)) %>% filter(!is.na(TLG)) both <- tl4_350 %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = 'Fillet & Whole Body', .groups='drop') alt <- tl4_350 %>% filter(TissueName == 'Whole Body') if(nrow(alt) > 0){ alt <- alt %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = TissueName[1], .groups='drop') } else{ alt <- both %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = NA, CompositeSamples = NA, CompositeTotalFish = NA, Years = NA, Tissue = 'Whole Body', .groups='drop') } tl4_350_results <- rbind(both, alt) #TL3 Fish (150-500 mm) tl3_500 <- dataframe %>% mutate(TLG = case_when(TrophicLevel =='TL3' & TLAvgLength >=150 & TLAvgLength <=500 ~ 'TL3 Fish (150-500 mm)', T ~ NA_character_)) %>% filter(!is.na(TLG)) both <- tl3_500 %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = 'Fillet & Whole Body', .groups='drop') alt <- tl3_500 %>% filter(TissueName == 'Fillet') if(nrow(alt) > 0){ alt <- alt %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = TissueName[1], .groups='drop') } else{ alt <- both %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = NA, CompositeSamples = NA, CompositeTotalFish = NA, Years = NA, Tissue = 'Fillet', .groups='drop') } tl3_500_results <- rbind(both, alt) #TL3 Fish (150-350 mm) tl3_350 <- dataframe %>% mutate(TLG = case_when(TrophicLevel =='TL3' & TLAvgLength >=150 & TLAvgLength <=350 ~ 'TL3 Fish (150-350 mm)', T ~ NA_character_)) %>% filter(!is.na(TLG)) both <- tl3_350 %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = 'Fillet & Whole Body', .groups='drop') alt <- tl3_350 %>% filter(TissueName == 'Whole Body') if(nrow(alt) > 0){ alt <- alt %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = TissueName[1], .groups='drop') } else{ alt <- both %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = NA, CompositeSamples = NA, CompositeTotalFish = NA, Years = NA, Tissue = 'Whole Body', .groups='drop') } tl3_350_results <- rbind(both, alt) #TL3 Fish (50 - <150 mm) tl3_150 <- dataframe %>% mutate(TLG = case_when(TrophicLevel =='TL3' & TLAvgLength >=50 & TLAvgLength < 150 ~ 'TL3 Fish (50 - <150 mm)', T ~ NA_character_)) %>% filter(!is.na(TLG)) both <- tl3_150 %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = 'Fillet & Whole Body', .groups='drop') alt <- tl3_150 %>% filter(TissueName == 'Whole Body') if(nrow(alt) > 0){ alt <- alt %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = TissueName[1], .groups='drop') } else{ alt <- both %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = NA, CompositeSamples = NA, CompositeTotalFish = NA, Years = NA, Tissue = 'Whole Body', .groups='drop') } tl3_150_results <- rbind(both, alt) #TL3 Fish (<50 mm) tl3_50 <- dataframe %>% mutate(TLG = case_when(TrophicLevel =='TL3' & TLAvgLength <50 ~ 'TL3 Fish (<50 mm)', T ~ NA_character_)) %>% filter(!is.na(TLG)) both <- tl3_50 %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = 'Fillet & Whole Body', .groups='drop') alt <- tl3_50 %>% filter(TissueName == 'Whole Body') if(nrow(alt) > 0){ alt <- alt %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = weighted.mean(Result, NumberFishPerComp), CompositeSamples = n(), CompositeTotalFish = sum(NumberFishPerComp), Years = paste(sort(unique(year(SampleDate))), collapse=', '), Tissue = TissueName[1], .groups='drop') } else{ alt <- both %>% group_by(TLG, !!! grouping_var) %>% summarise(CompositeWeightedAvg = NA, CompositeSamples = NA, CompositeTotalFish = NA, Years = NA, Tissue = 'Whole Body', .groups='drop') } tl3_50_results <- rbind(both, alt) # Bind results results <- rbind(tl4_500_results, tl4_350_results, tl3_500_results, tl3_350_results, tl3_150_results, tl3_50_results) %>% select(TLG, Tissue, everything()) return(results) }
The R session information (including the OS info, R version and all packages used):
sessionInfo()
## R version 4.2.2 (2022-10-31 ucrt) ## Platform: x86_64-w64-mingw32/x64 (64-bit) ## Running under: Windows 10 x64 (build 22621) ## ## Matrix products: default ## ## locale: ## [1] LC_COLLATE=English_United States.utf8 LC_CTYPE=English_United States.utf8 ## [3] LC_MONETARY=English_United States.utf8 LC_NUMERIC=C ## [5] LC_TIME=English_United States.utf8 ## ## attached base packages: ## [1] stats graphics grDevices utils datasets methods base ## ## other attached packages: ## [1] mgcv_1.8-41 nlme_3.1-160 lubridate_1.8.0 plotly_4.10.0 ## [5] readxl_1.4.1 actuar_3.3-0 NADA_1.6-1.1 forcats_0.5.2 ## [9] stringr_1.4.1 dplyr_1.0.9 purrr_0.3.4 readr_2.1.2 ## [13] tidyr_1.2.0 tibble_3.1.8 ggplot2_3.3.6 tidyverse_1.3.2 ## [17] fitdistrplus_1.1-8 survival_3.4-0 MASS_7.3-58.1 ## ## loaded via a namespace (and not attached): ## [1] lattice_0.20-45 assertthat_0.2.1 digest_0.6.29 utf8_1.2.2 ## [5] R6_2.5.1 cellranger_1.1.0 backports_1.4.1 reprex_2.0.2 ## [9] evaluate_0.16 highr_0.9 httr_1.4.4 pillar_1.8.1 ## [13] rlang_1.0.5 lazyeval_0.2.2 googlesheets4_1.0.1 rstudioapi_0.14 ## [17] data.table_1.14.2 Matrix_1.5-1 splines_4.2.2 googledrive_2.0.0 ## [21] htmlwidgets_1.5.4 munsell_0.5.0 broom_1.0.1 compiler_4.2.2 ## [25] modelr_0.1.9 xfun_0.32 pkgconfig_2.0.3 htmltools_0.5.3 ## [29] tidyselect_1.1.2 fansi_1.0.3 viridisLite_0.4.1 crayon_1.5.1 ## [33] tzdb_0.3.0 dbplyr_2.2.1 withr_2.5.0 grid_4.2.2 ## [37] jsonlite_1.8.0 gtable_0.3.1 lifecycle_1.0.1 DBI_1.1.3 ## [41] magrittr_2.0.3 scales_1.2.1 cli_3.3.0 stringi_1.7.8 ## [45] fs_1.5.2 xml2_1.3.3 ellipsis_0.3.2 generics_0.1.3 ## [49] vctrs_0.4.1 expint_0.1-7 tools_4.2.2 glue_1.6.2 ## [53] hms_1.1.2 fastmap_1.1.0 colorspace_2.0-3 gargle_1.2.0 ## [57] rvest_1.0.3 knitr_1.40 haven_2.5.1
Sys.time()
## [1] "2023-12-26 11:00:49 PST"