From a3041145458b8dca492f94a09dcb9e4d6e78a149 Mon Sep 17 00:00:00 2001 From: Garrick Aden-Buie Date: Wed, 2 Oct 2019 11:19:04 -0400 Subject: [PATCH] recode_age_groups() convert age group factor to character before recoding Fixes #82 --- R/age.R | 1 + tests/testthat/test-age.R | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/R/age.R b/R/age.R index 79afc5d..9532fe2 100644 --- a/R/age.R +++ b/R/age.R @@ -573,6 +573,7 @@ recode_age_groups <- function(data, breaks, age_group = age_group) { data %>% select(-dplyr::matches("age_min"), -dplyr::matches("age_max")) %>% dplyr::left_join(d_age_group, by = age_group_name) %>% + dplyr::mutate_at(dplyr::vars(!!age_group_name), as.character) %>% mutate(...age_group = dplyr::if_else( is.na(.data$...age_group), !!age_group, .data$...age_group )) %>% diff --git a/tests/testthat/test-age.R b/tests/testthat/test-age.R index dd53973..2f78164 100644 --- a/tests/testthat/test-age.R +++ b/tests/testthat/test-age.R @@ -472,6 +472,15 @@ describe("recode_age_groups()", { expect_equal(dplyr::group_vars(r_age_u_grouped), c("i", "unusual_name")) expect_equal(r_age_u_grouped$unusual_name, e_factor) }) + + it("handles age_group being a factor", { + d_age_groups$age_group <- factor(d_age_groups$age_group, ordered = TRUE) + + r_age_groups <- d_age_groups %>% + recode_age_groups(breaks = c(10, 20, 25)) + + expect_equal(r_age_groups$age_group, e_factor) + }) }) # Age Adjustment ----------------------------------------------------------