Skip to content

Commit

Permalink
recode_age_groups() convert age group factor to character before… (#85)
Browse files Browse the repository at this point in the history
recode_age_groups() convert age group factor to character before recoding
  • Loading branch information
gadenbuie authored Oct 2, 2019
2 parents 2c173cb + a304114 commit 35e2a78
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/age.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)) %>%
Expand Down
9 changes: 9 additions & 0 deletions tests/testthat/test-age.R
Original file line number Diff line number Diff line change
Expand Up @@ -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 ----------------------------------------------------------
Expand Down

0 comments on commit 35e2a78

Please sign in to comment.