Skip to content

Commit

Permalink
add test, docs
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Oct 2, 2024
1 parent fae8eed commit c33a8da
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
7 changes: 7 additions & 0 deletions R/categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@
#' x <- sample(1:10, size = 30, replace = TRUE)
#' categorize(x, "equal_length", n_groups = 3, labels = "mean")
#' categorize(x, "equal_length", n_groups = 3, labels = "median")
#'
#' # cut numeric into groups with the requested range as a label name
#' # each category has the same range, and labels indicate this range
#' categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "range")
#' # in this example, each category has the same range, but labels only refer
#' # to the ranges of the actual values (present in the data) inside each group
#' categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "observed")
#' @export
categorize <- function(x, ...) {
UseMethod("categorize")
Expand Down
7 changes: 7 additions & 0 deletions man/categorize.Rd

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

33 changes: 33 additions & 0 deletions tests/testthat/_snaps/categorize.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# categorize labelling ranged

Code
categorize(mtcars$mpg, "equal_length", n_groups = 5)
Output
[1] 3 3 3 3 2 2 1 3 3 2 2 2 2 2 1 1 1 5 5 5 3 2 2 1 2 4 4 5 2 2 1 3

---

Code
categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "range")
Output
[1] [19.8,24.5) [19.8,24.5) [19.8,24.5) [19.8,24.5) [15.1,19.8) [15.1,19.8)
[7] [10.4,15.1) [19.8,24.5) [19.8,24.5) [15.1,19.8) [15.1,19.8) [15.1,19.8)
[13] [15.1,19.8) [15.1,19.8) [10.4,15.1) [10.4,15.1) [10.4,15.1) [29.2,33.9]
[19] [29.2,33.9] [29.2,33.9] [19.8,24.5) [15.1,19.8) [15.1,19.8) [10.4,15.1)
[25] [15.1,19.8) [24.5,29.2) [24.5,29.2) [29.2,33.9] [15.1,19.8) [15.1,19.8)
[31] [10.4,15.1) [19.8,24.5)
Levels: [10.4,15.1) [15.1,19.8) [19.8,24.5) [24.5,29.2) [29.2,33.9]

---

Code
categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "observed")
Output
[1] (21-24.4) (21-24.4) (21-24.4) (21-24.4) (15.2-19.7) (15.2-19.7)
[7] (10.4-15) (21-24.4) (21-24.4) (15.2-19.7) (15.2-19.7) (15.2-19.7)
[13] (15.2-19.7) (15.2-19.7) (10.4-15) (10.4-15) (10.4-15) (30.4-33.9)
[19] (30.4-33.9) (30.4-33.9) (21-24.4) (15.2-19.7) (15.2-19.7) (10.4-15)
[25] (15.2-19.7) (26-27.3) (26-27.3) (30.4-33.9) (15.2-19.7) (15.2-19.7)
[31] (10.4-15) (21-24.4)
Levels: (10.4-15) (15.2-19.7) (21-24.4) (26-27.3) (30.4-33.9)

9 changes: 9 additions & 0 deletions tests/testthat/test-categorize.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,12 @@ test_that("categorize regex", {
categorize(mtcars, select = "mpg")
)
})


# select helpers ------------------------------
test_that("categorize labelling ranged", {
data(mtcars)
expect_snapshot(categorize(mtcars$mpg, "equal_length", n_groups = 5))
expect_snapshot(categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "range"))
expect_snapshot(categorize(mtcars$mpg, "equal_length", n_groups = 5, labels = "observed"))
})

0 comments on commit c33a8da

Please sign in to comment.