Skip to content

Commit

Permalink
Fix: ammended clean_ldn_region() to return London, when london inner/…
Browse files Browse the repository at this point in the history
…outer data is NA for latest year
  • Loading branch information
ArthurMurrell committed Feb 20, 2025
1 parent c815c22 commit b58f73b
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions R/fn_helper_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -359,24 +359,31 @@ mute_cat <- function(input) {
#'
#' @export
clean_ldn_region <- function(region, filtered_bds) {
# Return early if the region doesn't start with "London"
if (!grepl("^London", region)) {
# Return early if region is NA or doesn't start with "London"
if (is.na(region) || !stringr::str_starts(region, "London")) {
return(region)
}

# Extract values for the given region
ldn_values <- filtered_bds |>
dplyr::filter(`LA and Regions` == region) |>
# Check if required columns exist in filtered_bds
if (!all(c("LA and Regions", "values_num", "Years_num") %in% colnames(filtered_bds))) {
stop("filtered_bds must contain 'LA and Regions', 'values_num', and 'Years_num' columns")
}

# Determine the latest year available in the dataset
latest_year <- max(filtered_bds$Years_num, na.rm = TRUE)

# Extract values for the given region in the latest year
latest_values <- filtered_bds |>
dplyr::filter(`LA and Regions` == region, Years_num == latest_year) |>
dplyr::pull(values_num)

# Return "London" if all values are NA, otherwise return the original region
if (all(is.na(ldn_values))) {
# Return "London" if the value is NA for the latest year, otherwise return the original region
if (all(is.na(latest_values))) {
return("London")
} else {
return(region)
}
}

return(region)

Check notice

Code scanning / lintr

Use implicit return behavior; explicit return() is not needed. Note

Use implicit return behavior; explicit return() is not needed.
}

#' Retrieve AF Colours Without Warning Message
#'
Expand Down

0 comments on commit b58f73b

Please sign in to comment.