Skip to content

Commit

Permalink
Merge pull request #4 from lhdjung/devel
Browse files Browse the repository at this point in the history
debug and test `median_df()`
  • Loading branch information
lhdjung authored Dec 1, 2023
2 parents 548df54 + cf8bff2 commit 3581c01
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion R/median-table.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,6 @@ median_df <- function(x, even = c("mean", "low", "high"), ...) {
# stores the names of `x` if there are any:
tibble::tibble(
term = names(x), estimate, certainty, na_ignored, na_total, rate_ignored_na,
sum_total, rate_ignored_sum, stringsAsFactors = FALSE
sum_total, rate_ignored_sum
)
}
41 changes: 41 additions & 0 deletions tests/testthat/test-median-table.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@

# Example vectors ---------------------------------------------------------

vec1 <- list(
a = 1:15,
b = c(1, 1, NA),
c = c(4, 4, NA, NA, NA, NA),
d = c(96, 24, 3, NA)
)

vec2 <- iris[1:4]

vec1_exp <- tibble::tibble(
term = c("a", "b", "c", "d"),
estimate = c(8, 1, 4, 24),
certainty = c(TRUE, FALSE, FALSE, FALSE),
na_ignored = c(0L, 1L, 3L, 1L),
na_total = c(0L, 1L, 4L, 1L),
rate_ignored_na = c(NaN, 1, 0.75, 1),
sum_total = c(15L, 3L, 6L, 4L),
rate_ignored_sum = c(0, 0.3333333333333333, 0.5, 0.25),
)

vec2_exp <- tibble::tibble(
term = c("Sepal.Length", "Sepal.Width", "Petal.Length", "Petal.Width"),
estimate = c(5.8, 3, 4.35, 1.3),
certainty = rep(TRUE, 4L),
na_ignored = integer(4),
na_total = integer(4),
rate_ignored_na = rep(NaN, 4L),
sum_total = rep(150L, 4L),
rate_ignored_sum = numeric(4),
)


# Testing -----------------------------------------------------------------

test_that("`median_df()` works correctly", {
expect_equal(median_df(vec1), vec1_exp)
expect_equal(median_df(vec2), vec2_exp)
})

0 comments on commit 3581c01

Please sign in to comment.