# category_name <- "Bleaching" # Example category name
# input_coralhealth_colonies_data <- input_coralhealth_colonies_data %>% filter(SampleDate == "2005-09-26")
#
# input_coralhealth_condition_data <- input_coralhealth_condition_data
# input_benthiccodes <- input_benthiccodes
# default_condition_name <- NULL # Example default condition name
# exclude_conditions <- NULL # Example conditions to exclude
# values_function <- mean # Function to calculate values (mean, sum, etc.)
# output_type <- "extent" # Type of output (extent or prevalence)
process_health_data <-
function(category_name,
input_coralhealth_colonies_data,
input_coralhealth_condition_data,
input_benthiccodes,
default_condition_name = NULL,
exclude_conditions = NULL,
values_function = mean,
output_type = "extent",
minval = 0 # the minimum extent (in % in order to count as prevalence)
) {
# Step 1: Prepare the colony data
colony_data <- input_coralhealth_colonies_data %>%
select(colony_id,
Location,
SampleDate,
SurveyYear,
Period,
SampleType,
Method,
Transect,
Species) %>%
# filter(Period == "Annual") %>%
mutate(
# year = year(SampleDate),
year = SurveyYear,
program = "TCRMP",
replicatetype = "transect"
) %>%
rename(
coralspp = Species,
site = Location,
replicate = Transect,
date = SampleDate,
period = Period,
sampletype = SampleType,
method = Method
)
# Step 2: Join with condition data for the specified category
condition_data <- input_coralhealth_condition_data %>%
filter(category == category_name) %>%
select(colony_id, id, category, extent)
merged_data <- colony_data %>%
left_join(condition_data, by = "colony_id")
# Step 3: Join with benthic codes to get condition meanings
benthic_codes <- input_benthiccodes %>%
filter(Category == category_name) %>%
select(Code, Meaning)
final_data <- merged_data %>%
left_join(benthic_codes, by = c("id" = "Code")) %>%
mutate(
condition = ifelse(
is.na(Meaning),
ifelse(
is.null(default_condition_name),
paste0("No ", category_name),
default_condition_name
),
Meaning
)
# Do not replace NA extents; keep observed NAs
) %>%
select(
program,
site,
year,
date,
replicate,
replicatetype,
coralspp,
colony_id,
extent,
condition,
period,
sampletype,
method
) %>%
arrange(site, year, date, replicate, colony_id)
# Exclude specific conditions if needed
if (!is.null(exclude_conditions)) {
final_data <- final_data %>%
filter(!condition %in% exclude_conditions)
}
# july 28 2025 change extent from 0 to NA (or from NA to zero?) ah
# final_data_2 <- final_data %>%
# mutate(extent = ifelse(extent == 0, NA_real_, extent))
# mutate(extent = ifelse(is.na(extent), 0, extent))
# Step 4: Pivot the data to wide format
pivoted_data <- final_data %>%
pivot_wider(
names_from = condition,
values_from = extent,
values_fn = function(x) {
if (all(is.na(x))) {
NA # All extents are NA; keep as NA
} else {
values_function(x, na.rm = TRUE)
}
},
values_fill = NA # Fill missing combinations with zero
)
# Step 5: Adjust column names to lowercase and remove spaces
# Identify non-condition columns
non_condition_columns <- c(
"program",
"site",
"year",
"date",
"replicate",
"replicatetype",
"coralspp",
"colony_id",
"period",
"sampletype",
"method"
)
# Get condition columns
condition_columns <- setdiff(names(pivoted_data), non_condition_columns)
# Create a mapping of old names to new names
new_condition_names <- condition_columns %>%
set_names(.) %>%
map_chr(~ gsub(" ", "", tolower(.x)))
# # Rename the condition columns
# pivoted_data <- pivoted_data %>%
# rename_at(vars(condition_columns), ~ new_condition_names[.])
# Rename the condition columns using rename_with() and all_of()
pivoted_data <- pivoted_data %>%
rename_with( ~ new_condition_names[.], .cols = all_of(condition_columns))
# Update 'No category' and 'Any category' names
if (is.null(default_condition_name)) {
no_category_name <- gsub(" ", "", tolower(paste0("No ", category_name)))
} else {
no_category_name <- gsub(" ", "", tolower(default_condition_name))
}
any_category_name <- gsub(" ", "", tolower(paste0(
"Any ", category_name
)))
# Step 6: Calculate 'No category' and 'Any category'
# Exclude 'No category' column from sum
condition_columns_to_sum <- setdiff(
names(pivoted_data),
c(non_condition_columns, no_category_name)
)
# Handle cases where any condition extents are NA
pivoted_data <- pivoted_data %>%
rowwise() %>%
mutate(
total_extent = sum(across(all_of(
condition_columns_to_sum
)), na.rm = TRUE),
any_conditions_na = any(is.na(across(
all_of(condition_columns_to_sum)
))),
!!no_category_name := sum(c(100, -total_extent), na.rm = TRUE),
!!any_category_name := total_extent
) %>%
ungroup() %>%
select(-total_extent, -any_conditions_na)
# Step 7: Convert to prevalence if output_type says so
if (output_type == "prevalence") {
# Convert extent columns to prevalence
prevalence_data <- pivoted_data %>%
mutate(
across(
all_of(
c(
condition_columns_to_sum,
no_category_name,
any_category_name
)
),
# ~ case_when(
# . == 0 ~ 0,
# # Extent is 0, prevalence is 0
# is.na(.) ~ 1,
# # Extent is NA, prevalence is 1
# . > 0 ~ 1
# # Extent > 0, prevalence is 1
# ))) |>
# select(-all_of(no_category_name)) # Remove 'No category' column
# pivoted_data <- prevalence_data
~ case_when(
. <= minval ~ 0,
# Extent is less than or equal to minval, prevalence is 0
. > minval ~ 1,
# Extent is > minval, prevalence is 1
is.na(.) ~ 0,
# Extent is NA, prevalence is 0
# . > 0 ~ 1
# Extent > 0, prevalence is 1
))) |>
select(-all_of(no_category_name)) # Remove 'No category' column
pivoted_data <- prevalence_data
}
return(pivoted_data)
}
# NEW
process_health_data <-
function(category_name,
input_coralhealth_colonies_data,
input_coralhealth_condition_data,
input_benthiccodes,
default_condition_name = NULL,
exclude_conditions = NULL,
values_function = mean,
output_type = "extent",
minval = 0 # the minimum extent (in % in order to count as prevalence)
) {
# Step 1: Prepare the colony data
colony_data <- input_coralhealth_colonies_data %>%
select(colony_id,
Location,
SampleDate,
SurveyYear,
Period,
SampleType,
Method,
Transect,
Species) %>%
# filter(Period == "Annual") %>%
mutate(
# year = year(SampleDate),
year = SurveyYear,
program = "TCRMP",
replicatetype = "transect"
) %>%
rename(
coralspp = Species,
site = Location,
replicate = Transect,
date = SampleDate,
period = Period,
sampletype = SampleType,
method = Method
)
# Step 2: Join with condition data for the specified category
condition_data <- input_coralhealth_condition_data %>%
filter(category == category_name) %>%
select(colony_id, id, category, extent)
merged_data <- colony_data %>%
left_join(condition_data, by = "colony_id")
# Step 3: Join with benthic codes to get condition meanings
benthic_codes <- input_benthiccodes %>%
filter(Category == category_name) %>%
select(Code, Meaning)
final_data <- merged_data %>%
left_join(benthic_codes, by = c("id" = "Code")) %>%
mutate(
condition = ifelse(
is.na(Meaning),
ifelse(
is.null(default_condition_name),
paste0("No ", category_name),
default_condition_name
),
Meaning
)
# Do not replace NA extents; keep observed NAs
) %>%
select(
program,
site,
year,
date,
replicate,
replicatetype,
coralspp,
colony_id,
extent,
condition,
period,
sampletype,
method
) %>%
arrange(site, year, date, replicate, colony_id)
# Exclude specific conditions if needed
if (!is.null(exclude_conditions)) {
final_data <- final_data %>%
filter(!condition %in% exclude_conditions)
}
# Step 4: Pivot the data to wide format
pivoted_data <- final_data %>%
pivot_wider(
names_from = condition,
values_from = extent,
values_fn = function(x) {
if (all(is.na(x))) {
NA # All extents are NA; keep as NA
} else {
values_function(x, na.rm = TRUE)
}
},
values_fill = NA # Fill missing combinations with NA
)
# Step 5: Adjust column names to lowercase and remove spaces
non_condition_columns <- c(
"program",
"site",
"year",
"date",
"replicate",
"replicatetype",
"coralspp",
"colony_id",
"period",
"sampletype",
"method"
)
condition_columns <- setdiff(names(pivoted_data), non_condition_columns)
new_condition_names <- condition_columns %>%
set_names(.) %>%
map_chr(~ gsub(" ", "", tolower(.x)))
pivoted_data <- pivoted_data %>%
rename_with(~ new_condition_names[.], .cols = all_of(condition_columns))
# Update 'No category' and 'Any category' names
if (is.null(default_condition_name)) {
no_category_name <- gsub(" ", "", tolower(paste0("No ", category_name)))
} else {
no_category_name <- gsub(" ", "", tolower(default_condition_name))
}
any_category_name <- gsub(" ", "", tolower(paste0("Any ", category_name)))
# Step 6: Calculate 'No category' and 'Any category' (vectorized)
condition_columns_to_sum <- setdiff(
names(pivoted_data),
c(non_condition_columns, no_category_name)
)
if (length(condition_columns_to_sum) > 0) {
mat <- as.matrix(pivoted_data[, condition_columns_to_sum, drop = FALSE])
total_extent <- rowSums(mat, na.rm = TRUE)
} else {
# If nothing to sum, total is 0 for all rows (matches previous behavior)
total_extent <- rep(0, nrow(pivoted_data))
}
pivoted_data[[no_category_name]] <- 100 - total_extent
pivoted_data[[any_category_name]] <- total_extent
# Step 7: Convert to prevalence if output_type says so
if (output_type == "prevalence") {
prevalence_data <- pivoted_data %>%
mutate(
across(
all_of(c(condition_columns_to_sum, no_category_name, any_category_name)),
~ case_when(
. <= minval ~ 0,
. > minval ~ 1,
is.na(.) ~ 0
)
)
) %>%
select(-all_of(no_category_name)) # Remove 'No category' column
pivoted_data <- prevalence_data
}
return(pivoted_data)
}