Biological Condition Gradient level assignment (1st and 2nd) given Level memberships.

BCG.Level.Assignment(
  df.level.membership,
  col_SampleID = "SAMPLEID",
  col_Index_Name = "INDEX_NAME",
  col_Index_Class = "INDEX_CLASS",
  col_L1 = "L1",
  col_L2 = "L2",
  col_L3 = "L3",
  col_L4 = "L4",
  col_L5 = "L5",
  col_L6 = "L6"
)

Arguments

df.level.membership

Wide data frame with level memberships (0-1).

col_SampleID

Column name for sample id. Default = "SAMPLEID"

col_Index_Name

Column name for index name. Default = "INDEX_NAME"

col_Index_Class

Column name for index class. Default = "INDEX_CLASS"

col_L1

Column name for memberships, Level 1. Default = "L1"

col_L2

Column name for memberships, Level 2. Default = "L2"

col_L3

Column name for memberships, Level 3. Default = "L3"

col_L4

Column name for memberships, Level 4. Default = "L4"

col_L5

Column name for memberships, Level 5. Default = "L5"

col_L6

Column name for memberships, Level 6. Default = "L6"

Value

Returns a data frame of results in the wide format.

Details

Input is L1 to L6 with membership values of 0 to 1. Result is 1st Level (Primary_BCG_Level) and 2nd Level (Secondary_BCG_Level). Also give close (Membership_Close) and a continuous proportional Level assignment ("Continuous_BCG_Level").

Examples

# Example 1

# construct a dummy dataset
L1 <- c(rep(0, 12))
L2 <- c(0.4, 0, 0.4, rep(0,7), 0, 0)
L3 <- c(0.6, 0, 0.6, 0, 0.42, 0, 1, 1, 0.22, 0.33, 0.5, 0)
L4 <- c(0, 0.9, 0, 0, 0.58, 0.05, 0, 0, 0.78, 0.67, 0.5, 0)
L5 <- c(0, 0.1, 0, 1, 0, 0.95, rep(0,4), 0, 1)
L6 <- c(rep(0, length(L1)))
SAMPLEID <- LETTERS[1:length(L1)]
df_lev_memb <- data.frame(SAMPLEID = SAMPLEID
                          , INDEX_NAME = "TEST_NAME"
                          , INDEX_CLASS = "TEST_CLASS"
                          , L1 = L1
                          , L2 = L2
                          , L3 = L3
                          , L4 = L4
                          , L5 = L5
                          , L6 = L6)

# Run Function
df_Levels <- BCG.Level.Assignment(df_lev_memb)

# Show Results
#View(df_Levels)

# Save Results
write.table(df_Levels
            , file.path(tempdir(), "Levels.tsv")
            , row.names = FALSE
            , col.names = TRUE
            , sep = "\t")

#~~~~~~~~~~~~~~~~~~~~~~~

# Example 2

# library(readxl)
# library(reshape2) 
# library(BioMonTools)

# Calculate Metrics
df_samps_bugs <- readxl::read_excel(system.file(
                                       "extdata/Data_BCG_PugLowWilVal.xlsx"
                                        , package="BCGcalc")
                           , guess_max = 10^6)
                                        
# Run Function
myDF <- df_samps_bugs
myCols <- c("Area_mi2", "SurfaceArea", "Density_m2", "Density_ft2")
#' # populate missing columns prior to metric calculation
col_missing <- c("INFRAORDER", "HABITAT", "ELEVATION_ATTR", "GRADIENT_ATTR"
                 , "WSAREA_ATTR", "HABSTRUCT", "UFC")
myDF[, col_missing] <- NA
df_met_val_bugs <- BioMonTools::metric.values(myDF
                                              , "bugs"
                                              , fun.cols2keep = myCols) 
#> Joining with `by = join_by(SAMPLEID, INDEX_NAME, INDEX_CLASS)`

# Import Rules
df_rules <- readxl::read_excel(system.file("extdata/Rules.xlsx"
                                           , package="BCGcalc")
                              , sheet="Rules") 
#> Warning: Expecting numeric in C1980 / R1980C3: got 'Level 2'
#> Warning: Expecting numeric in C1981 / R1981C3: got 'Level 2'
#> Warning: Expecting numeric in C1982 / R1982C3: got 'Level 2'
#> Warning: Expecting numeric in C1983 / R1983C3: got 'Level 2'
#> Warning: Expecting numeric in C1984 / R1984C3: got 'Level 2'
#> Warning: Expecting numeric in C1985 / R1985C3: got 'Level 2'
#> Warning: Expecting numeric in C1986 / R1986C3: got 'Level 2'
#> Warning: Expecting numeric in C1987 / R1987C3: got 'Level 2'
#> Warning: Expecting numeric in C1988 / R1988C3: got 'Level 2'
#> Warning: Expecting numeric in C1989 / R1989C3: got 'Level 2'
#> Warning: Expecting numeric in C1990 / R1990C3: got 'Level 3'
#> Warning: Expecting numeric in C1991 / R1991C3: got 'Level 3'
#> Warning: Expecting numeric in C1992 / R1992C3: got 'Level 3'
#> Warning: Expecting numeric in C1993 / R1993C3: got 'Level 3'
#> Warning: Expecting numeric in C1994 / R1994C3: got 'Level 3'
#> Warning: Expecting numeric in C1995 / R1995C3: got 'Level 3'
#> Warning: Expecting numeric in C1996 / R1996C3: got 'Level 3'
#> Warning: Expecting numeric in C1997 / R1997C3: got 'Level 3'
#> Warning: Expecting numeric in C1998 / R1998C3: got 'Level 4'
#> Warning: Expecting numeric in C1999 / R1999C3: got 'Level 4'
#> Warning: Expecting numeric in C2000 / R2000C3: got 'Level 4'
#> Warning: Expecting numeric in C2001 / R2001C3: got 'Level 4'
#> Warning: Expecting numeric in C2002 / R2002C3: got 'Level 4'
#> Warning: Expecting numeric in C2003 / R2003C3: got 'Level 4'
#> Warning: Expecting numeric in C2004 / R2004C3: got 'Level 4'
#> Warning: Expecting numeric in C2005 / R2005C3: got 'Level 5'
#> Warning: Expecting numeric in C2006 / R2006C3: got 'Level 5'
#> Warning: Expecting numeric in C2007 / R2007C3: got 'Level 5'
#> Warning: Expecting numeric in C2008 / R2008C3: got 'Level 2'
#> Warning: Expecting numeric in C2009 / R2009C3: got 'Level 2'
#> Warning: Expecting numeric in C2010 / R2010C3: got 'Level 2'
#> Warning: Expecting numeric in C2011 / R2011C3: got 'Level 2'
#> Warning: Expecting numeric in C2012 / R2012C3: got 'Level 2'
#> Warning: Expecting numeric in C2013 / R2013C3: got 'Level 2'
#> Warning: Expecting numeric in C2014 / R2014C3: got 'Level 2'
#> Warning: Expecting numeric in C2015 / R2015C3: got 'Level 2'
#> Warning: Expecting numeric in C2016 / R2016C3: got 'Level 2'
#> Warning: Expecting numeric in C2017 / R2017C3: got 'Level 2'
#> Warning: Expecting numeric in C2018 / R2018C3: got 'Level 3'
#> Warning: Expecting numeric in C2019 / R2019C3: got 'Level 3'
#> Warning: Expecting numeric in C2020 / R2020C3: got 'Level 3'
#> Warning: Expecting numeric in C2021 / R2021C3: got 'Level 3'
#> Warning: Expecting numeric in C2022 / R2022C3: got 'Level 3'
#> Warning: Expecting numeric in C2023 / R2023C3: got 'Level 3'
#> Warning: Expecting numeric in C2024 / R2024C3: got 'Level 3'
#> Warning: Expecting numeric in C2025 / R2025C3: got 'Level 3'
#> Warning: Expecting numeric in C2026 / R2026C3: got 'Level 4'
#> Warning: Expecting numeric in C2027 / R2027C3: got 'Level 4'
#> Warning: Expecting numeric in C2028 / R2028C3: got 'Level 4'
#> Warning: Expecting numeric in C2029 / R2029C3: got 'Level 4'
#> Warning: Expecting numeric in C2030 / R2030C3: got 'Level 4'
#> Warning: Expecting numeric in C2031 / R2031C3: got 'Level 4'
#> Warning: Expecting numeric in C2032 / R2032C3: got 'Level 4'
#> Warning: Expecting numeric in C2033 / R2033C3: got 'Level 5'
#> Warning: Expecting numeric in C2034 / R2034C3: got 'Level 5'
#> Warning: Expecting numeric in C2035 / R2035C3: got 'Level 5'
#> Warning: Expecting numeric in C2036 / R2036C3: got 'Level 2'
#> Warning: Expecting numeric in C2037 / R2037C3: got 'Level 2'
#> Warning: Expecting numeric in C2038 / R2038C3: got 'Level 2'
#> Warning: Expecting numeric in C2039 / R2039C3: got 'Level 2'
#> Warning: Expecting numeric in C2040 / R2040C3: got 'Level 2'
#> Warning: Expecting numeric in C2041 / R2041C3: got 'Level 2'
#> Warning: Expecting numeric in C2042 / R2042C3: got 'Level 2'
#> Warning: Expecting numeric in C2043 / R2043C3: got 'Level 2'
#> Warning: Expecting numeric in C2044 / R2044C3: got 'Level 2'
#> Warning: Expecting numeric in C2045 / R2045C3: got 'Level 2'
#> Warning: Expecting numeric in C2046 / R2046C3: got 'Level 3'
#> Warning: Expecting numeric in C2047 / R2047C3: got 'Level 3'
#> Warning: Expecting numeric in C2048 / R2048C3: got 'Level 3'
#> Warning: Expecting numeric in C2049 / R2049C3: got 'Level 3'
#> Warning: Expecting numeric in C2050 / R2050C3: got 'Level 3'
#> Warning: Expecting numeric in C2051 / R2051C3: got 'Level 3'
#> Warning: Expecting numeric in C2052 / R2052C3: got 'Level 3'
#> Warning: Expecting numeric in C2053 / R2053C3: got 'Level 3'
#> Warning: Expecting numeric in C2054 / R2054C3: got 'Level 3'
#> Warning: Expecting numeric in C2055 / R2055C3: got 'Level 4'
#> Warning: Expecting numeric in C2056 / R2056C3: got 'Level 4'
#> Warning: Expecting numeric in C2057 / R2057C3: got 'Level 4'
#> Warning: Expecting numeric in C2058 / R2058C3: got 'Level 4'
#> Warning: Expecting numeric in C2059 / R2059C3: got 'Level 4'
#> Warning: Expecting numeric in C2060 / R2060C3: got 'Level 4'
#> Warning: Expecting numeric in C2061 / R2061C3: got 'Level 4'
#> Warning: Expecting numeric in C2062 / R2062C3: got 'Level 4'
#> Warning: Expecting numeric in C2063 / R2063C3: got 'Level 5'
#> Warning: Expecting numeric in C2064 / R2064C3: got 'Level 5'
#> Warning: Expecting numeric in C2065 / R2065C3: got 'Level 5'
#> Warning: Expecting numeric in C2066 / R2066C3: got 'Level 2'
#> Warning: Expecting numeric in C2067 / R2067C3: got 'Level 2'
#> Warning: Expecting numeric in C2068 / R2068C3: got 'Level 2'
#> Warning: Expecting numeric in C2069 / R2069C3: got 'Level 2'
#> Warning: Expecting numeric in C2070 / R2070C3: got 'Level 2'
#> Warning: Expecting numeric in C2071 / R2071C3: got 'Level 2'
#> Warning: Expecting numeric in C2072 / R2072C3: got 'Level 2'
#> Warning: Expecting numeric in C2073 / R2073C3: got 'Level 2'
#> Warning: Expecting numeric in C2074 / R2074C3: got 'Level 2'
#> Warning: Expecting numeric in C2075 / R2075C3: got 'Level 3'
#> Warning: Expecting numeric in C2076 / R2076C3: got 'Level 3'
#> Warning: Expecting numeric in C2077 / R2077C3: got 'Level 3'
#> Warning: Expecting numeric in C2078 / R2078C3: got 'Level 3'
#> Warning: Expecting numeric in C2079 / R2079C3: got 'Level 3'
#> Warning: Expecting numeric in C2080 / R2080C3: got 'Level 3'
#> Warning: Expecting numeric in C2081 / R2081C3: got 'Level 3'
#> Warning: Expecting numeric in C2082 / R2082C3: got 'Level 4'
#> Warning: Expecting numeric in C2083 / R2083C3: got 'Level 4'
#> Warning: Expecting numeric in C2084 / R2084C3: got 'Level 4'
#> Warning: Expecting numeric in C2085 / R2085C3: got 'Level 5'
#> Warning: Expecting numeric in C2086 / R2086C3: got 'Level 2'
#> Warning: Expecting numeric in C2087 / R2087C3: got 'Level 2'
#> Warning: Expecting numeric in C2088 / R2088C3: got 'Level 2'
#> Warning: Expecting numeric in C2089 / R2089C3: got 'Level 2'
#> Warning: Expecting numeric in C2090 / R2090C3: got 'Level 2'
#> Warning: Expecting numeric in C2091 / R2091C3: got 'Level 2'
#> Warning: Expecting numeric in C2092 / R2092C3: got 'Level 2'
#> Warning: Expecting numeric in C2093 / R2093C3: got 'Level 2'
#> Warning: Expecting numeric in C2094 / R2094C3: got 'Level 3'
#> Warning: Expecting numeric in C2095 / R2095C3: got 'Level 3'
#> Warning: Expecting numeric in C2096 / R2096C3: got 'Level 3'
#> Warning: Expecting numeric in C2097 / R2097C3: got 'Level 3'
#> Warning: Expecting numeric in C2098 / R2098C3: got 'Level 3'
#> Warning: Expecting numeric in C2099 / R2099C3: got 'Level 4'
#> Warning: Expecting numeric in C2100 / R2100C3: got 'Level 4'
#> Warning: Expecting numeric in C2101 / R2101C3: got 'Level 4'
#> Warning: Expecting numeric in C2102 / R2102C3: got 'Level 5'
#> Warning: Expecting numeric in C2103 / R2103C3: got 'Level 2'
#> Warning: Expecting numeric in C2104 / R2104C3: got 'Level 2'
#> Warning: Expecting numeric in C2105 / R2105C3: got 'Level 2'
#> Warning: Expecting numeric in C2106 / R2106C3: got 'Level 2'
#> Warning: Expecting numeric in C2107 / R2107C3: got 'Level 2'
#> Warning: Expecting numeric in C2108 / R2108C3: got 'Level 2'
#> Warning: Expecting numeric in C2109 / R2109C3: got 'Level 2'
#> Warning: Expecting numeric in C2110 / R2110C3: got 'Level 2'
#> Warning: Expecting numeric in C2111 / R2111C3: got 'Level 2'
#> Warning: Expecting numeric in C2112 / R2112C3: got 'Level 2'
#> Warning: Expecting numeric in C2113 / R2113C3: got 'Level 2'
#> Warning: Expecting numeric in C2114 / R2114C3: got 'Level 2'
#> Warning: Expecting numeric in C2115 / R2115C3: got 'Level 2'
#> Warning: Expecting numeric in C2116 / R2116C3: got 'Level 3'
#> Warning: Expecting numeric in C2117 / R2117C3: got 'Level 3'
#> Warning: Expecting numeric in C2118 / R2118C3: got 'Level 3'
#> Warning: Expecting numeric in C2119 / R2119C3: got 'Level 3'
#> Warning: Expecting numeric in C2120 / R2120C3: got 'Level 3'
#> Warning: Expecting numeric in C2121 / R2121C3: got 'Level 3'
#> Warning: Expecting numeric in C2122 / R2122C3: got 'Level 3'
#> Warning: Expecting numeric in C2123 / R2123C3: got 'Level 3'
#> Warning: Expecting numeric in C2124 / R2124C3: got 'Level 3'
#> Warning: Expecting numeric in C2125 / R2125C3: got 'Level 3'
#> Warning: Expecting numeric in C2126 / R2126C3: got 'Level 3'
#> Warning: Expecting numeric in C2127 / R2127C3: got 'Level 4'
#> Warning: Expecting numeric in C2128 / R2128C3: got 'Level 4'
#> Warning: Expecting numeric in C2129 / R2129C3: got 'Level 4'
#> Warning: Expecting numeric in C2130 / R2130C3: got 'Level 4'
#> Warning: Expecting numeric in C2131 / R2131C3: got 'Level 4'
#> Warning: Expecting numeric in C2132 / R2132C3: got 'Level 4'
#> Warning: Expecting numeric in C2133 / R2133C3: got 'Level 4'
#> Warning: Expecting numeric in C2134 / R2134C3: got 'Level 4'
#> Warning: Expecting numeric in C2135 / R2135C3: got 'Level 5'
#> Warning: Expecting numeric in C2136 / R2136C3: got 'Level 5'
#> Warning: Expecting numeric in C2137 / R2137C3: got 'Level 5'
#> Warning: Expecting numeric in C2138 / R2138C3: got 'Level 5'
#> Warning: Expecting numeric in C2139 / R2139C3: got 'Level 5'
#> Warning: Expecting numeric in C2140 / R2140C3: got 'Level 2'
#> Warning: Expecting numeric in C2141 / R2141C3: got 'Level 2'
#> Warning: Expecting numeric in C2142 / R2142C3: got 'Level 2'
#> Warning: Expecting numeric in C2143 / R2143C3: got 'Level 2'
#> Warning: Expecting numeric in C2144 / R2144C3: got 'Level 2'
#> Warning: Expecting numeric in C2145 / R2145C3: got 'Level 2'
#> Warning: Expecting numeric in C2146 / R2146C3: got 'Level 2'
#> Warning: Expecting numeric in C2147 / R2147C3: got 'Level 2'
#> Warning: Expecting numeric in C2148 / R2148C3: got 'Level 2'
#> Warning: Expecting numeric in C2149 / R2149C3: got 'Level 2'
#> Warning: Expecting numeric in C2150 / R2150C3: got 'Level 3'
#> Warning: Expecting numeric in C2151 / R2151C3: got 'Level 3'
#> Warning: Expecting numeric in C2152 / R2152C3: got 'Level 3'
#> Warning: Expecting numeric in C2153 / R2153C3: got 'Level 3'
#> Warning: Expecting numeric in C2154 / R2154C3: got 'Level 3'
#> Warning: Expecting numeric in C2155 / R2155C3: got 'Level 3'
#> Warning: Expecting numeric in C2156 / R2156C3: got 'Level 3'
#> Warning: Expecting numeric in C2157 / R2157C3: got 'Level 3'
#> Warning: Expecting numeric in C2158 / R2158C3: got 'Level 3'
#> Warning: Expecting numeric in C2159 / R2159C3: got 'Level 4'
#> Warning: Expecting numeric in C2160 / R2160C3: got 'Level 4'
#> Warning: Expecting numeric in C2161 / R2161C3: got 'Level 4'
#> Warning: Expecting numeric in C2162 / R2162C3: got 'Level 4'
#> Warning: Expecting numeric in C2163 / R2163C3: got 'Level 4'
#> Warning: Expecting numeric in C2164 / R2164C3: got 'Level 4'
#> Warning: Expecting numeric in C2165 / R2165C3: got 'Level 4'
#> Warning: Expecting numeric in C2166 / R2166C3: got 'Level 4'
#> Warning: Expecting numeric in C2167 / R2167C3: got 'Level 5'
#> Warning: Expecting numeric in C2168 / R2168C3: got 'Level 5'
#> Warning: Expecting numeric in C2169 / R2169C3: got 'Level 5'
#> Warning: Expecting numeric in C2170 / R2170C3: got 'Level 5'

# Calculate Metric Memberships
df_met_memb <- BCG.Metric.Membership(df_met_val_bugs, df_rules)

# Calculate Level Memberships
df_lev_memb <- BCG.Level.Membership(df_met_memb, df_rules)
#> Warning: There were 14 warnings in `dplyr::summarize()`.
#> The first warning was:
#>  In argument: `rules_lev_min = min(LEVEL, na.rm = TRUE)`.
#>  In group 11: `INDEX_NAME = "BCG_ORWA_v1_Bugs500ct"`, `INDEX_CLASS =
#>   "foothills_multihab"`.
#> Caused by warning in `min()`:
#> ! no non-missing arguments to min; returning Inf
#>  Run `dplyr::last_dplyr_warnings()` to see the 13 remaining warnings.

# Run Function
df_Levels <- BCG.Level.Assignment(df_lev_memb)

# QC Checks (flags)
#
# Import Checks
df_checks <- readxl::read_excel(system.file("extdata/MetricFlags.xlsx"
                                            , package="BCGcalc")
                               , sheet="Flags") 

# Run Function
df_flags <- BioMonTools::qc.checks(df_met_val_bugs, df_checks)
# Change terminology; PASS/FAIL to NA/flag
df_flags[, "FLAG"][df_flags[, "FLAG"] == "FAIL"] <- "flag"
df_flags[, "FLAG"][df_flags[, "FLAG"] == "PASS"] <- NA

# long to wide format
df_flags_wide <- reshape2::dcast(df_flags
                                 , SAMPLEID ~ CHECKNAME
                                 , value.var="FLAG")
# Calc number of "flag"s by row.
df_flags_wide$NumFlags <- rowSums(df_flags_wide == "flag", na.rm = TRUE)
# Rearrange columns
NumCols <- ncol(df_flags_wide)
df_flags_wide <- df_flags_wide[, c(1, NumCols, 2:(NumCols - 1))]

# Merge Levels and Flags
df_lev_flags <- merge(df_Levels
                      , df_flags_wide
                      , by.x = "SampleID"
                      , by.y = "SAMPLEID"
                      , all.x = TRUE)
             
# Summarize Results
table(df_flags[, "CHECKNAME"], df_flags[, "FLAG"], useNA = "ifany")
#>                                  
#>                                   flag <NA>
#>   Low density (ft2)                  0  678
#>   Low density (m2)                   0  678
#>   Ramellogammarus                    8  670
#>   brackish organisms present         7  671
#>   catchment, Large                   0  678
#>   catchment, small                 158  520
#>   individuals, Large                 0  678
#>   individuals, dominant 02, Large  204  474
#>   individuals, small                80  598
#>   surface area, small              112  566
table(df_lev_flags$BCG_Status)
#> 
#>   2 2.5   3 3.5   4 4.5   5 5.5   6 
#>  42   5 304  33 125   2 123   1  43 

# Show Results
# View(df_lev_flags)

# Save Results
write.csv(df_lev_flags, file.path(tempdir(), "Level_Flags.csv"))

# # Summary Report
# strFile.RMD <- system.file(paste0("rmd/Results_Summary.Rmd")
#                              , package = "BCGcalc")
# strFile.RMD.format <- "html_document"
# strFile.out <- "_bcgcalc_RESULTS.html"
# dir.export <- tempdir()
# rmarkdown::render(strFile.RMD
#                    , output_format = strFile.RMD.format
#                    , output_file = strFile.out
#                    , output_dir = dir.export
#                    , quiet = TRUE)