Skip to content

Commit

Permalink
Add convert_to_age_band()
Browse files Browse the repository at this point in the history
  • Loading branch information
izaak-jephson committed Jul 23, 2024
1 parent 07762b8 commit 80c16e1
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 3 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RoxygenNote: 7.3.1
Imports:
bizdays,
dplyr,
glue,
janitor,
lubridate,
magrittr,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export(adorn_financial_years)
export(bucket_other)
export(convert_col_date)
export(convert_date)
export(convert_to_age_band)
export(create_sss_calendar)
export(financial_year)
export(make_all_number_percent)
Expand Down
61 changes: 60 additions & 1 deletion R/manipulate_data.R
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#' Transposes a Data Frame
#'
#' This function take a data frame or tibble and transposes it, turning columns to
#'rows and rows to columns
#' rows and rows to columns
#' @param data A data frame to transpose.
#' @param pivot_column Column to be turned into column headings. Defaults to the
#' first column in the data frame.
Expand All @@ -14,3 +14,62 @@ transpose_data <- function(data, pivot_column = 1, names_to = "name"){
tidyr::pivot_longer(-pivot_column, names_to = names_to) %>%
tidyr::pivot_wider(names_from = pivot_column)
}

#' Adds a Coloumn of Age Bands to a Data Frame
#'
#' This function takes data frame and adds a column of age bands based on a column
#' of ages, according to a given age band specification.
#' @param data A data frame
#' @param age_col Column containing age data
#' @param age_breaks A vector of breaks in age bands
#' @param na Value to convert nas to. Defaults to "Unknown".
#' @export

convert_to_age_band <- function(data, age_col, age_breaks, na = "Unknown"){

age_band_spec <-
create_age_band_spec(age_breaks) %>%
dplyr::mutate(
cond = glue::glue('{age_col} >= "{low}" & {age_col} < "{high}" ~ "{name}"'),
cond = rlang::parse_exprs(.data$cond)
)

data %>%
dplyr::mutate(
age_band = dplyr::case_when(!!!age_band_spec$cond)
) %>%
dplyr::mutate(
age_band = dplyr::case_when(
is.na(age_band) ~ na,
TRUE ~ age_band)
)
}

#' Converts List of Ages to Age Band Specification
#'
#' @param ages Vector of breaks in age bands
#'
#'

create_age_band_spec <- function(ages){

low_limit <-
dplyr::tibble(name = paste("Under", ages[1]),
low = 0,
high = ages[1])

high_limit <-
dplyr::tibble(name = paste(ages[length(ages)], "and over"),
low = ages[length(ages)],
high = 150)

limits <-
dplyr::tibble(name = paste0(ages[-length(ages)], "-", ages[-1] - 1),
low = ages[-length(ages)],
high = ages[-1])

dplyr::bind_rows(
low_limit, limits, high_limit
)

}
2 changes: 2 additions & 0 deletions R/summarise_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,5 @@ adorn_financial_years <- function(data, month, financial_years){
dplyr::arrange(.data$is_fin_year) %>%
dplyr::select(-c(.data$is_fin_year))
}


2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Security Scotland statistics team.

## Using the package

To install the package directly from GitHUb, open RStudio and run:
To install the package directly from GitHub, open RStudio and run:

devtools::install_github("ScotGovAnalysis/sssstats",
upgrade = "never",
Expand Down
21 changes: 21 additions & 0 deletions man/convert_to_age_band.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions man/create_age_band_spec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ create_test_data_2 <- function(cols = tidyselect::everything()){
dplyr::tibble(place = c("Aberdeen", "Glasgow", "Edinburgh", "Edinburgh", "Ayr", "Perth", "Stirling", "Inverness", "Glasgow", "Edinburgh"),
month = c("January 2024", "February 2023", "September 2025", "October 2024", "March 2023", "February 2022", "September 2022", "December 2024", "November 2024", "April 2022"),
count = c(4, 5, 7, 3, 6, 2, 7, 3, 4, 9),
value = c(450, 399, 233, 736, 182, 433, 469, 932, 102, 377)) %>%
value = c(450, 399, 233, 736, 182, 433, 469, 932, 102, 377),
age = count * 5 + 1) %>%
dplyr::select(all_of(cols))
}

0 comments on commit 80c16e1

Please sign in to comment.