Skip to content

Commit a628941

Browse files
committed
create_agegroups(): padding improvements
1 parent e7cc8bd commit a628941

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

R/create_agegroups.R

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,31 @@
1414
#' @param breaks_as_lower_bound Logical; if \code{TRUE} (default), breaks define the the lower bounds of the intervals (e.g., a break at 5 starts the '5-9' group).
1515
#' If \code{FALSE}, breaks define the upper bound (e.g., a break at 5 ends the '0-5' group).
1616
#' @param first_group_format Character string template for the first age group. Uses [glue::glue] syntax. \cr
17-
#' The variable `x` represents the upper bound of the first interval.\cr
17+
#' The variable `x` represents the upper bound of the first interval.
18+
#' Ignored if the first group is collapsed using `collapse_single_year_groups`. \cr
1819
#' Default: \code{"0-{x}"}. Other common styles: \code{"<={x}", "<{x+1}"}
1920
#' @param interval_format Character string template for intermediate age groups. Uses [glue::glue] syntax.\cr
2021
#' The variables `x` and `y` represent the lower and upper bounds of the interval, respectively.\cr
2122
#' Default: \code{"{x}-{y}"}. Other common styles: \code{"{x} to {y}"}
2223
#' @param last_group_format Character string template for the last age group. Uses [glue::glue] syntax. \cr
2324
#' The variable `x` represents the lower bound of the last interval.\cr
2425
#' Default: \code{"{x}+"}. Other common styles: \code{">={x}",">{x-1}"}
25-
#' @param pad_numbers Logical or numeric; if numeric, pad numbers up to the specified length (Tip: use \code{2}).
26+
#' @param pad_numbers Logical or numeric; if numeric, pad numbers up to the specified length
27+
#' (`TRUE` will be treated as `2`).
2628
#' Not compatible with calculations within glue formats. Default: \code{FALSE}
2729
#' @param pad_with Character to use for padding numbers. Default: \code{"0"}
2830
#' @param collapse_single_year_groups Logical; if \code{TRUE}, groups spanning a single year (e.g., from `age_breaks = c(1, 2)`)
2931
#' are formatted as a single number (e.g., "1") instead of a range (e.g., "1-1"). Default: \code{FALSE}
30-
#' @param na_label Label for \code{NA} values. If \code{NA}, keeps default \code{NA} handling. Default: \code{NA}
31-
#' @param return_factor Logical; if \code{TRUE}, returns a factor, if \code{FALSE} returns character vector. Default: \code{FALSE}
32+
#' @param na_label Label for \code{NA} values (including negative ages). If \code{NA}, keeps default \code{NA} handling. Default: \code{NA}
33+
#' @param return_factor Logical; if \code{TRUE}, returns a factor, if \code{FALSE} returns character vector.
34+
#' Can be used to keep the ordering and all possible levels of the age groups. Default: \code{FALSE}
3235
#'
3336
#' @return Vector of age group labels (character or factor depending on return_factor)
3437
#'
38+
#' @details
39+
#' Values below 0 are treated as `NA`.
40+
#'
41+
#'
3542
#' @examples
3643
#' # Basic usage
3744
#' create_agegroups(1:100)
@@ -74,6 +81,7 @@ create_agegroups <- function(
7481
age_breaks <- sort(unique(floor(age_breaks)))
7582
age_breaks <- age_breaks[age_breaks != 0]
7683
breaks <- c(-Inf, age_breaks, Inf)
84+
pad_numbers <- ifelse(is_bool(pad_numbers) & pad_numbers, 2L, pad_numbers)
7785

7886
if (any(values < 0, na.rm = TRUE)) {
7987
cli::cli_warn("Negative ages detected. These will be treated as NA.")
@@ -100,7 +108,9 @@ create_agegroups <- function(
100108
labels <- c(
101109
# First Group
102110
case_when(
103-
collapse_single_year_groups & (age_breaks[1] + corr_up == 0) ~ "0",
111+
# Collapse groups if specified
112+
collapse_single_year_groups & (age_breaks[1] + corr_up == 0) ~
113+
stringr::str_pad("0", as.numeric(pad_numbers), pad = pad_with),
104114
TRUE ~ write_labels(first_group_format, age_breaks[1] + corr_up),
105115
),
106116
# Mid group when more than 1 breaks was supplied
@@ -109,7 +119,7 @@ create_agegroups <- function(
109119
# Collapse groups if specified
110120
(collapse_single_year_groups &
111121
(age_breaks[1:(length(age_breaks) - 1)] + corr_low == age_breaks[2:length(age_breaks)] + corr_up)
112-
) ~ as.character(age_breaks[1:(length(age_breaks) - 1)] + corr_low),
122+
) ~ stringr::str_pad((age_breaks[1:(length(age_breaks) - 1)] + corr_low), as.numeric(pad_numbers), pad = pad_with),
113123
# Default
114124
TRUE ~ write_labels(
115125
interval_format,

man/create_agegroups.Rd

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-create_agegroups.R

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ test_that("different formatting options work", {
4040
),
4141
c("0-01", "02-03", "02-03", "04-09", "04-09", "10+")
4242
)
43+
44+
expect_identical(
45+
create_agegroups(c(1:5, 10),
46+
age_breaks = c(1:5, 10),
47+
pad_numbers = TRUE, collapse_single_year_groups = TRUE
48+
),
49+
c("01", "02", "03", "04", "05-09", "10+")
50+
)
51+
52+
expect_identical(
53+
create_agegroups(c(1:5, 10),
54+
age_breaks = c(1:5, 10),
55+
pad_numbers = 3, collapse_single_year_groups = TRUE
56+
),
57+
c("001", "002", "003", "004", "005-009", "010+")
58+
)
4359
})
4460

4561
test_that("single year group collapsing works", {

0 commit comments

Comments
 (0)