Skip to content

Commit

Permalink
Add test for bucket_other()
Browse files Browse the repository at this point in the history
  • Loading branch information
izaak-jephson committed Jul 22, 2024
1 parent 481d2d3 commit 590df6e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
5 changes: 2 additions & 3 deletions R/summarise_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@
#' @param other Value to be substituted for non-allowed values. Defaults to "Other"
#' @export


bucket_other <- function(data, column, allowed_values, other = "Other"){
data %>%
dplyr::mutate({{column}} := dplyr::case_when(
{{column}} %in% allowed_values ~
{{column}},
.data[[column]] %in% allowed_values ~
.data[[column]],
.default = other))
}

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/helper.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ create_test_data <- function(cols = tidyselect::everything()){
#' Helper function to create test data set 2
#' @export
create_test_data_2 <- function(cols = tidyselect::everything()){
dplyr::tibble(place = c("Aberdeen", "Glasgow", "Edinburgh", "Mull", "Ayr", "Perth", "Stirling", "Inverness", "Shetland", "Arran"),
dplyr::tibble(place = c("Aberdeen", "Glasgow", "Edinburgh", "Edinburgh", "Ayr", "Perth", "Stirling", "Inverness", "Glasgow", "Edinburgh"),
month = c("January 2024", "February 2023", "September 2025", "October 2024", "March 2023", "February 2022", "September 2022", "December 2024", "November 2024", "April 2022"),
count = c(4, 5, 7, 3, 6, 2, 7, 3, 4, 9),
value = c(450, 399, 233, 736, 182, 433, 469, 932, 102, 377)) %>%
Expand Down
8 changes: 8 additions & 0 deletions tests/testthat/test-summarise_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
test_that("bucket_other() aggregates any values not allowed in argument", {
test_data <- create_test_data_2(c("place", "count"))
expect_equal(bucket_other(data = test_data,
col = "place",
allowed_values = c("Glasgow", "Edinburgh")),
dplyr::tibble(place = c("Other", "Glasgow", "Edinburgh", "Edinburgh", "Other", "Other", "Other", "Other", "Glasgow", "Edinburgh"),
count = c(4, 5, 7, 3, 6, 2, 7, 3, 4, 9)))
})

0 comments on commit 590df6e

Please sign in to comment.