Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add notation argument to limits_to_agegroups() #137

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
35 changes: 26 additions & 9 deletions R/limits_to_agegroups.r
Original file line number Diff line number Diff line change
Expand Up @@ -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")) {
sbfnk marked this conversation as resolved.
Show resolved Hide resolved
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], ")")
}, "")
sbfnk marked this conversation as resolved.
Show resolved Hide resolved
} 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
}
Expand Down
11 changes: 9 additions & 2 deletions man/limits_to_agegroups.Rd

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

14 changes: 11 additions & 3 deletions tests/testthat/test-agegroups.r
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,17 @@
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)

Check warning on line 7 in tests/testthat/test-agegroups.r

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-agegroups.r,line=7,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
expect_warning(limits_to_agegroups(groups), "default")
age_groups <-
expect_equal(

Check warning on line 10 in tests/testthat/test-agegroups.r

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-agegroups.r,line=10,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
as.character(unique(limits_to_agegroups(groups, notation = "brackets"))),
c("[0,5)", "[5,10)", "10+")
)
expect_equal(

Check warning on line 14 in tests/testthat/test-agegroups.r

View workflow job for this annotation

GitHub Actions / lint-changed-files

file=tests/testthat/test-agegroups.r,line=14,col=3,[expect_identical_linter] Use expect_identical(x, y) by default; resort to expect_equal() only when needed, e.g. when setting ignore_attr= or tolerance=.
as.character(unique(limits_to_agegroups(groups, notation = "dashes"))),
c("0-4", "5-9", "10+")
)
})

test_that("age groups are ordered factors", {
Expand Down
Loading