Skip to content

Commit

Permalink
Merge branch 'main' into rescale_weights_kish
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke authored Dec 23, 2024
2 parents f54a939 + c2d3825 commit 6e14888
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/testthat/_snaps/data_tabulate.md
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,23 @@
---------+------------+------------+------
Total | 40 | 46 | 86

---

Code
print(data_tabulate(efc, c("c172code", "e42dep"), by = "e16sex", proportions = "row"))
Output
Variable | Value | male | female | <NA> | Total
---------+-------+------------+------------+------------+------
c172code | 1 | 5 (62.5%) | 2 (25.0%) | 1 (12.5%) | 8
c172code | 2 | 31 (47.0%) | 33 (50.0%) | 2 (3.0%) | 66
c172code | 3 | 4 (25.0%) | 11 (68.8%) | 1 (6.2%) | 16
c172code | NA | 5 (50.0%) | 4 (40.0%) | 1 (10.0%) | 10
e42dep | 1 | 2 (100.0%) | 0 (0.0%) | 0 (0.0%) | 2
e42dep | 2 | 2 (50.0%) | 2 (50.0%) | 0 (0.0%) | 4
e42dep | 3 | 8 (28.6%) | 18 (64.3%) | 2 (7.1%) | 28
e42dep | 4 | 32 (50.8%) | 28 (44.4%) | 3 (4.8%) | 63
e42dep | NA | 1 (33.3%) | 2 (66.7%) | 0 (0.0%) | 3

# data_tabulate, cross tables, grouped df

Code
Expand Down Expand Up @@ -529,6 +546,46 @@
attr(,"class")
[1] "knitr_kable" "character"

---

Code
print_md(data_tabulate(efc, "c172code", by = "e16sex", proportions = "column",
remove_na = TRUE, weights = "weights"))
Output
[1] "|c172code | male| female| Total|"
[2] "|:--------|----------:|----------:|-----:|"
[3] "|1 | 5 (12.5%)| 3 (6.5%)| 8|"
[4] "|2 | 32 (80.0%)| 32 (69.6%)| 64|"
[5] "|3 | 3 (7.5%)| 11 (23.9%)| 14|"
[6] "| | | | |"
[7] "|Total | 40| 46| 86|"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"

---

Code
print_md(data_tabulate(efc, c("c172code", "e42dep"), by = "e16sex",
proportions = "row"))
Output
[1] "|Variable | Value| male| female| (NA)| Total|"
[2] "|:--------|-----:|----------:|----------:|----------:|-----:|"
[3] "|c172code | 1| 5 (62.5%)| 2 (25.0%)| 1 (12.5%)| 8|"
[4] "|c172code | 2| 31 (47.0%)| 33 (50.0%)| 2 (3.0%)| 66|"
[5] "|c172code | 3| 4 (25.0%)| 11 (68.8%)| 1 (6.2%)| 16|"
[6] "|c172code | NA| 5 (50.0%)| 4 (40.0%)| 1 (10.0%)| 10|"
[7] "|e42dep | 1| 2 (100.0%)| 0 (0.0%)| 0 (0.0%)| 2|"
[8] "|e42dep | 2| 2 (50.0%)| 2 (50.0%)| 0 (0.0%)| 4|"
[9] "|e42dep | 3| 8 (28.6%)| 18 (64.3%)| 2 (7.1%)| 28|"
[10] "|e42dep | 4| 32 (50.8%)| 28 (44.4%)| 3 (4.8%)| 63|"
[11] "|e42dep | NA| 1 (33.3%)| 2 (66.7%)| 0 (0.0%)| 3|"
attr(,"format")
[1] "pipe"
attr(,"class")
[1] "knitr_kable" "character"

# data_tabulate, correct 0% for proportions

Code
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-data_tabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ test_that("data_tabulate, cross tables", {
expect_snapshot(print(data_tabulate(efc, "c172code", by = "e16sex", proportions = "column", remove_na = TRUE)))
expect_snapshot(print(data_tabulate(efc, "c172code", by = "e16sex", proportions = "column", weights = "weights")))
expect_snapshot(print(data_tabulate(efc, "c172code", by = "e16sex", proportions = "column", remove_na = TRUE, weights = "weights"))) # nolint
expect_snapshot(print(data_tabulate(efc, c("c172code", "e42dep"), by = "e16sex", proportions = "row"))) # nolint
})

test_that("data_tabulate, cross tables, HTML", {
Expand All @@ -341,6 +342,7 @@ test_that("data_tabulate, cross tables, grouped df", {
expect_snapshot(print(data_tabulate(grp, "c172code", by = "e16sex", proportions = "row")))
skip_if_not_installed("gt")
expect_s3_class(print_html(data_tabulate(grp, "c172code", by = "e16sex", proportions = "row")), "gt_tbl") # nolint
expect_s3_class(print_html(data_tabulate(efc, c("e16sex", "e42dep"), by = "c172code", proportions = "row")), "gt_tbl") # nolint
})

test_that("data_tabulate, cross tables, errors by", {
Expand Down Expand Up @@ -381,6 +383,8 @@ test_that("data_tabulate, cross tables, markdown", {
expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "full", remove_na = TRUE)))
expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "full", weights = efc$weights)))
expect_snapshot(print_md(data_tabulate(efc$c172code, by = efc$e16sex, proportions = "full", remove_na = TRUE, weights = efc$weights))) # nolint
expect_snapshot(print_md(data_tabulate(efc, "c172code", by = "e16sex", proportions = "column", remove_na = TRUE, weights = "weights"))) # nolint
expect_snapshot(print_md(data_tabulate(efc, c("c172code", "e42dep"), by = "e16sex", proportions = "row"))) # nolint
})


Expand Down

0 comments on commit 6e14888

Please sign in to comment.