Skip to content

Commit c1fc702

Browse files
Merge pull request #37 from OxfordIHTM/dev
refactor calculate age function and add tests
2 parents ab70a2d + 8c3190f commit c1fc702

File tree

8 files changed

+85
-10
lines changed

8 files changed

+85
-10
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Generated by roxygen2: do not edit by hand
22

33
export(cod_calculate_age)
4+
export(cod_calculate_ages)
45
export(cod_check_age)
56
export(cod_recode_age_type)
67
export(cod_recode_sex)
78
export(get_age_values)
9+
importFrom(dplyr,bind_rows)
810
importFrom(dplyr,case_when)
911
importFrom(dplyr,mutate)
1012
importFrom(dplyr,select)

R/cod_calculate_age.R

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,22 @@ cod_calculate_age <- function(dob,
7373
## Return age_df ----
7474
age_df
7575
}
76+
77+
#'
78+
#' @rdname cod_calculate_age
79+
#' @export
80+
#'
81+
82+
cod_calculate_ages <- function(dob,
83+
dod,
84+
date_format = "%Y-%m-%d",
85+
codedit = TRUE) {
86+
Map(
87+
f = cod_calculate_age,
88+
dob = dob,
89+
dod = dod,
90+
date_format = date_format,
91+
codedit = codedit
92+
) |>
93+
dplyr::bind_rows()
94+
}

R/cod_check_sex.R

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,15 @@
33
#'
44
#' @param sex_value An integer value or vector of values for age based on the
55
#' CoDEdit rules.
6+
#' @param sex_code A character or integer vector of 2 values that indicate
7+
#' which values are to be considered pertaining to males (first value in the
8+
#' vector) or to females (second value in the vector).
69
#'
710

8-
cod_check_sex <- function(sex_value) {
11+
cod_check_sex <- function(sex_value, sex_code) {
12+
## Recode sex ----
13+
sex_value <- cod_recode_sex(sex_value = sex_value, sex_code = sex_code)
14+
915
## Create sex_check vector ----
1016
sex_check <- vector(mode = "integer", length = length(sex_value))
1117

@@ -14,22 +20,20 @@ cod_check_sex <- function(sex_value) {
1420

1521
## Check that sex_value is either 1L for males, 2L for females, and 9L for
1622
## unknown
17-
sex_check <- ifelse(
18-
any(!sex_value %in% c(1L, 2L, 9L)), sex_check + 2L, sex_check
19-
)
20-
23+
sex_check <- ifelse(all(!sex_value %in% c(1L, 2L, 9L)), 2L, sex_check)
2124

2225
## Check if sex_value is missing ----
23-
sex_check <- ifelse(is.na(sex_value), sex_check + 4L, sex_check)
26+
sex_check <- ifelse(is.na(sex_value), 3L, sex_check)
2427

2528
## Create sex_check note vector ----
2629
sex_check_note <- vector(mode = "character", length = length(sex_value))
2730

2831
sex_check_note[sex_check == 0] <- "No issues with sex values"
2932
sex_check_note[sex_check == 1] <- "Sex value is not an integer"
3033
sex_check_note[sex_check == 2] <- "Sex value is not any of the expected values"
31-
sex_check_note[sex_check == 3] <- "Sex value is not an integer; Sex value is not any of the expected values"
32-
sex_check_note[sex_check == 4] <- "Missing sex value"
33-
sex_check_note[sex_check == 5] <- ""
34+
sex_check_note[sex_check == 3] <- "Missing sex value"
35+
3436

37+
## Return check ----
38+
tibble::tibble(sex_check, sex_check_note)
3539
}

R/codeditr-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#' @name codeditr
1616
#' @importFrom methods is
1717
#' @importFrom tibble tibble
18-
#' @importFrom dplyr mutate select case_when
18+
#' @importFrom dplyr mutate select case_when bind_rows
1919
#' @importFrom rlang .data
2020
#'
2121
"_PACKAGE"

man/cod_calculate_age.Rd

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

man/cod_check_sex.Rd

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

pkgdown/_pkgdown.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,12 @@ reference:
5555
- title: Calculate
5656
contents:
5757
- cod_calculate_age
58+
- cod_calculate_ages
5859

5960
- title: Check
6061
contents:
6162
- cod_check_age
63+
- cod_check_sex
6264
- cod_check_missing
6365

6466
- title: Utilities
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Tests for calculating age function -------------------------------------------
2+
3+
dob <- c("1977-11-05", "1971-04-04", "2012-08-13")
4+
dod <- Sys.Date()
5+
6+
7+
testthat::test_that(
8+
"output is as expected", {
9+
expect_s3_class(cod_calculate_ages(dob = dob, dod = dod), "tbl")
10+
11+
expect_named(
12+
cod_calculate_ages(dob = dob, dod = dod),
13+
expected = c("age_value", "age_type")
14+
)
15+
16+
expect_s3_class(
17+
cod_calculate_ages(dob = dob, dod = dod, codedit = FALSE),
18+
"tbl"
19+
)
20+
21+
expect_named(
22+
cod_calculate_ages(dob = dob, dod = dod, codedit = FALSE),
23+
expected = c("age_days", "age_months", "age_years")
24+
)
25+
}
26+
)

0 commit comments

Comments
 (0)