From 741235fd805754a5c3f4fd258cd334574227c0b7 Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Thu, 26 Sep 2024 20:55:25 +0100 Subject: [PATCH 1/5] add `notation` argument to limits_to_agegroups --- NEWS.md | 1 + R/limits_to_agegroups.r | 35 ++++++++++++++++++++++++++--------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/NEWS.md b/NEWS.md index 795d4dc..b67ece0 100644 --- a/NEWS.md +++ b/NEWS.md @@ -3,6 +3,7 @@ * The speed of loading surveys has been increased. * An error has been fixed causing NA contact matrices if any 5-year age band in the population data was missing. * Results of function calls accessing Zenodo repository are now cached for speedup and to avoid multiple web requests +* The `limits_to_agegroups` has been changed to return bracket notated age ranges by default # socialmixr 0.3.2 diff --git a/R/limits_to_agegroups.r b/R/limits_to_agegroups.r index b0e6740..5c01126 100644 --- a/R/limits_to_agegroups.r +++ b/R/limits_to_agegroups.r @@ -3,20 +3,37 @@ #' Mostly used for plot labelling #' @param x age limits to transform #' @param limits lower age limits; if not given, will use all limits in `x` -#' @return Age groups (limits separated by dashes) +#' @param notation whether to use bracket notation, e.g. [0,4) or dash +#' notation, e.g. 0-4) +#' @return Age groups as specified in `notation` #' @examples #' limits_to_agegroups(c(0, 5, 10)) #' @export -limits_to_agegroups <- function(x, limits = sort(unique(x))) { +limits_to_agegroups <- function(x, limits = sort(unique(x)), + notation = c("dashes", "brackets")) { + if (missing(notation)) { + warning( + "In the next version of socialmixr the default notation will ", + "become \"brackets\" instead of \"dashes\". To prevent this, ", + "use `notation = \"dashes\"` in the call to `limits_to_agegroups()`." + ) + } + notation <- match.arg(notation) limits <- limits[!is.na(limits)] agegroups <- if (length(limits) > 1) { - vapply(seq(1, length(limits) - 1), function(y) { - if ((limits[y + 1] - 1) > limits[y]) { - paste(limits[y], limits[y + 1] - 1, sep = "-") - } else { - paste(limits[y]) - } - }, "") + if (notation == "brackets") { + vapply(seq(1, length(limits) - 1), function(y) { + paste0("[", limits[y], ",", limits[y + 1], ")") + }, "") + } else if (notation == "dashes") { + vapply(seq(1, length(limits) - 1), function(y) { + if ((limits[y + 1] - 1) > limits[y]) { + paste(limits[y], limits[y + 1] - 1, sep = "-") + } else { + paste(limits[y]) + } + }, "") + } } else { NULL } From afa39a3825312cc0e3d4dc4ca938450ee68de7b2 Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Thu, 26 Sep 2024 21:05:36 +0100 Subject: [PATCH 2/5] update tests --- tests/testthat/test-agegroups.r | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-agegroups.r b/tests/testthat/test-agegroups.r index d3f20ed..3e2873f 100644 --- a/tests/testthat/test-agegroups.r +++ b/tests/testthat/test-agegroups.r @@ -4,9 +4,17 @@ test_that("age groups can be created and manipulated", { ages <- seq_len(50) age_limits <- c(0, 5, 10) groups <- reduce_agegroups(ages, age_limits) - expect_length(unique(groups), 3) - age_groups <- limits_to_agegroups(groups) - expect_length(unique(age_groups), 3) + expect_equal(unique(groups), age_limits) + expect_warning(limits_to_agegroups(groups), "default") + age_groups <- + expect_equal( + as.character(unique(limits_to_agegroups(groups, notation = "brackets"))), + c("[0,5)", "[5,10)", "10+") + ) + expect_equal( + as.character(unique(limits_to_agegroups(groups, notation = "dashes"))), + c("0-4", "5-9", "10+") + ) }) test_that("age groups are ordered factors", { From 67a56fe98ead52024e60e8751af39b7d52bd65d9 Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Thu, 26 Sep 2024 21:09:19 +0100 Subject: [PATCH 3/5] update documentation --- man/limits_to_agegroups.Rd | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/man/limits_to_agegroups.Rd b/man/limits_to_agegroups.Rd index 05a3e0a..f4291fc 100644 --- a/man/limits_to_agegroups.Rd +++ b/man/limits_to_agegroups.Rd @@ -4,15 +4,22 @@ \alias{limits_to_agegroups} \title{Convert lower age limits to age groups.} \usage{ -limits_to_agegroups(x, limits = sort(unique(x))) +limits_to_agegroups( + x, + limits = sort(unique(x)), + notation = c("dashes", "brackets") +) } \arguments{ \item{x}{age limits to transform} \item{limits}{lower age limits; if not given, will use all limits in \code{x}} + +\item{notation}{whether to use bracket notation, e.g. [0,4) or dash +notation, e.g. 0-4)} } \value{ -Age groups (limits separated by dashes) +Age groups as specified in \code{notation} } \description{ Mostly used for plot labelling From 20f2f0cbe8de9260710351660bcf2ae2f96aef80 Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Tue, 8 Oct 2024 09:20:43 +0100 Subject: [PATCH 4/5] improve code for making brackets Co-authored-by: Hugo Gruson <10783929+Bisaloo@users.noreply.github.com> --- R/limits_to_agegroups.r | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/R/limits_to_agegroups.r b/R/limits_to_agegroups.r index 5c01126..e07725e 100644 --- a/R/limits_to_agegroups.r +++ b/R/limits_to_agegroups.r @@ -22,9 +22,7 @@ limits_to_agegroups <- function(x, limits = sort(unique(x)), limits <- limits[!is.na(limits)] agegroups <- if (length(limits) > 1) { if (notation == "brackets") { - vapply(seq(1, length(limits) - 1), function(y) { - paste0("[", limits[y], ",", limits[y + 1], ")") - }, "") + sprintf("[%s,%s)", limits[-length(limits)], limits[-1]) } else if (notation == "dashes") { vapply(seq(1, length(limits) - 1), function(y) { if ((limits[y + 1] - 1) > limits[y]) { From 220c761b077fb63ce893f08fb7da335179457efe Mon Sep 17 00:00:00 2001 From: Sebastian Funk Date: Fri, 11 Oct 2024 10:36:39 +0100 Subject: [PATCH 5/5] changes requested by lintr --- tests/testthat/test-agegroups.r | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/testthat/test-agegroups.r b/tests/testthat/test-agegroups.r index 3e2873f..efb3595 100644 --- a/tests/testthat/test-agegroups.r +++ b/tests/testthat/test-agegroups.r @@ -4,14 +4,14 @@ test_that("age groups can be created and manipulated", { ages <- seq_len(50) age_limits <- c(0, 5, 10) groups <- reduce_agegroups(ages, age_limits) - expect_equal(unique(groups), age_limits) + expect_identical(unique(groups), age_limits) expect_warning(limits_to_agegroups(groups), "default") age_groups <- - expect_equal( + expect_identical( as.character(unique(limits_to_agegroups(groups, notation = "brackets"))), c("[0,5)", "[5,10)", "10+") ) - expect_equal( + expect_identical( as.character(unique(limits_to_agegroups(groups, notation = "dashes"))), c("0-4", "5-9", "10+") )