Skip to content

Commit

Permalink
align values in tables
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Feb 13, 2024
1 parent 06146e5 commit 12eb590
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 74 deletions.
24 changes: 9 additions & 15 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,29 +108,23 @@ format.dw_data_xtabulate <- function(x, format = "text", digits = 1, big_mark =
tmp <- x
if (identical(props, "row")) {
for (i in seq_len(nrow(x))) {
tmp[i, -1] <- sprintf(
"%s (%.*f%%)",
x[i, -1],
digits,
100 * x[i, -1] / sum(x[i, -1], na.rm = TRUE)
tmp[i, -1] <- paste(
format(x[i, -1]),
format(sprintf("(%.*f%%)", digits, 100 * x[i, -1] / sum(x[i, -1], na.rm = TRUE)), justify = "right")
)
}
} else if (identical(props, "column")) {
for (i in seq_len(ncol(x))[-1]) {
tmp[, i] <- sprintf(
"%s (%.*f%%)",
x[, i],
digits,
100 * x[, i] / sum(x[, i], na.rm = TRUE)
tmp[, i] <- paste(
format(x[, i]),
format(sprintf("(%.*f%%)", digits, 100 * x[, i] / sum(x[, i], na.rm = TRUE)), justify = "right")
)
}
} else if (identical(props, "cell")) {
for (i in seq_len(ncol(x))[-1]) {
tmp[, i] <- sprintf(
"%s (%.*f%%)",
x[, i],
digits,
100 * x[, i] / total_n
tmp[, i] <- paste(
format(x[, i]),
format(sprintf("(%.*f%%)", digits, 100 * x[, i] / total_n), justify = "right")
)
}
}
Expand Down
118 changes: 59 additions & 59 deletions tests/testthat/_snaps/data_tabulate.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@
Output
efc$c172code | male | female | NA | Total
-------------+------------+------------+----------+------
1 | 5 (5.0%) | 2 (2.0%) | 1 (1.0%) | 8
1 | 5 (5.0%) | 2 (2.0%) | 1 (1.0%) | 8
2 | 31 (31.0%) | 33 (33.0%) | 2 (2.0%) | 66
3 | 4 (4.0%) | 11 (11.0%) | 1 (1.0%) | 16
<NA> | 5 (5.0%) | 4 (4.0%) | 1 (1.0%) | 10
3 | 4 (4.0%) | 11 (11.0%) | 1 (1.0%) | 16
<NA> | 5 (5.0%) | 4 (4.0%) | 1 (1.0%) | 10
-------------+------------+------------+----------+------
Total | 45 | 50 | 5 | 100

Expand All @@ -263,9 +263,9 @@
Output
efc$c172code | male | female | Total
-------------+------------+------------+------
1 | 5 (5.8%) | 2 (2.3%) | 7
1 | 5 (5.8%) | 2 (2.3%) | 7
2 | 31 (36.0%) | 33 (38.4%) | 64
3 | 4 (4.7%) | 11 (12.8%) | 15
3 | 4 (4.7%) | 11 (12.8%) | 15
-------------+------------+------------+------
Total | 40 | 46 | 86

Expand All @@ -277,10 +277,10 @@
Output
efc$c172code | male | female | NA | Total
-------------+------------+------------+----------+------
1 | 5 (4.8%) | 3 (2.9%) | 2 (1.9%) | 10
1 | 5 (4.8%) | 3 (2.9%) | 2 (1.9%) | 10
2 | 32 (30.5%) | 32 (30.5%) | 3 (2.9%) | 67
3 | 3 (2.9%) | 11 (10.5%) | 1 (1.0%) | 15
<NA> | 8 (7.6%) | 5 (4.8%) | 1 (1.0%) | 14
3 | 3 (2.9%) | 11 (10.5%) | 1 (1.0%) | 15
<NA> | 8 (7.6%) | 5 (4.8%) | 1 (1.0%) | 14
-------------+------------+------------+----------+------
Total | 48 | 51 | 7 | 105

Expand All @@ -292,9 +292,9 @@
Output
efc$c172code | male | female | Total
-------------+------------+------------+------
1 | 5 (5.8%) | 3 (3.5%) | 8
1 | 5 (5.8%) | 3 (3.5%) | 8
2 | 32 (37.2%) | 32 (37.2%) | 64
3 | 3 (3.5%) | 11 (12.8%) | 14
3 | 3 (3.5%) | 11 (12.8%) | 14
-------------+------------+------------+------
Total | 40 | 46 | 86

Expand All @@ -306,8 +306,8 @@
c172code | male | female | NA | Total
---------+------------+------------+-----------+------
1 | 5 (62.5%) | 2 (25.0%) | 1 (12.5%) | 8
2 | 31 (47.0%) | 33 (50.0%) | 2 (3.0%) | 66
3 | 4 (25.0%) | 11 (68.8%) | 1 (6.2%) | 16
2 | 31 (47.0%) | 33 (50.0%) | 2 (3.0%) | 66
3 | 4 (25.0%) | 11 (68.8%) | 1 (6.2%) | 16
<NA> | 5 (50.0%) | 4 (40.0%) | 1 (10.0%) | 10
---------+------------+------------+-----------+------
Total | 45 | 50 | 5 | 100
Expand Down Expand Up @@ -337,9 +337,9 @@
c172code | male | female | NA | Total
---------+------------+------------+-----------+------
1 | 5 (50.0%) | 3 (30.0%) | 2 (20.0%) | 10
2 | 32 (47.8%) | 32 (47.8%) | 3 (4.5%) | 67
3 | 3 (20.0%) | 11 (73.3%) | 1 (6.7%) | 15
<NA> | 8 (57.1%) | 5 (35.7%) | 1 (7.1%) | 14
2 | 32 (47.8%) | 32 (47.8%) | 3 (4.5%) | 67
3 | 3 (20.0%) | 11 (73.3%) | 1 (6.7%) | 15
<NA> | 8 (57.1%) | 5 (35.7%) | 1 (7.1%) | 14
---------+------------+------------+-----------+------
Total | 48 | 51 | 7 | 105
Expand All @@ -366,10 +366,10 @@
Output
c172code | male | female | NA | Total
---------+------------+------------+-----------+------
1 | 5 (11.1%) | 2 (4.0%) | 1 (20.0%) | 8
1 | 5 (11.1%) | 2 (4.0%) | 1 (20.0%) | 8
2 | 31 (68.9%) | 33 (66.0%) | 2 (40.0%) | 66
3 | 4 (8.9%) | 11 (22.0%) | 1 (20.0%) | 16
<NA> | 5 (11.1%) | 4 (8.0%) | 1 (20.0%) | 10
3 | 4 (8.9%) | 11 (22.0%) | 1 (20.0%) | 16
<NA> | 5 (11.1%) | 4 (8.0%) | 1 (20.0%) | 10
---------+------------+------------+-----------+------
Total | 45 | 50 | 5 | 100
Expand All @@ -382,7 +382,7 @@
Output
c172code | male | female | Total
---------+------------+------------+------
1 | 5 (12.5%) | 2 (4.3%) | 7
1 | 5 (12.5%) | 2 (4.3%) | 7
2 | 31 (77.5%) | 33 (71.7%) | 64
3 | 4 (10.0%) | 11 (23.9%) | 15
---------+------------+------------+------
Expand All @@ -397,10 +397,10 @@
Output
c172code | male | female | NA | Total
---------+------------+------------+-----------+------
1 | 5 (10.4%) | 3 (5.9%) | 2 (28.6%) | 10
1 | 5 (10.4%) | 3 (5.9%) | 2 (28.6%) | 10
2 | 32 (66.7%) | 32 (62.7%) | 3 (42.9%) | 67
3 | 3 (6.2%) | 11 (21.6%) | 1 (14.3%) | 15
<NA> | 8 (16.7%) | 5 (9.8%) | 1 (14.3%) | 14
3 | 3 (6.2%) | 11 (21.6%) | 1 (14.3%) | 15
<NA> | 8 (16.7%) | 5 (9.8%) | 1 (14.3%) | 14
---------+------------+------------+-----------+------
Total | 48 | 51 | 7 | 105
Expand All @@ -413,9 +413,9 @@
Output
c172code | male | female | Total
---------+------------+------------+------
1 | 5 (12.5%) | 3 (6.5%) | 8
1 | 5 (12.5%) | 3 (6.5%) | 8
2 | 32 (80.0%) | 32 (69.6%) | 64
3 | 3 (7.5%) | 11 (23.9%) | 14
3 | 3 (7.5%) | 11 (23.9%) | 14
---------+------------+------------+------
Total | 40 | 46 | 86
Expand All @@ -427,52 +427,52 @@
Output
Grouped by e42dep (1)
c172code | male | NA | Total
---------+------------+----------+------
2 | 2 (100.0%) | 0 (0.0%) | 2
<NA> | 0 (NaN%) | 0 (NaN%) | 0
---------+------------+----------+------
Total | 2 | 0 | 2
c172code | male | NA | Total
---------+------------+------------+------
2 | 2 (100.0%) | 0 (0.0%) | 2
<NA> | 0 (NaN%) | 0 (NaN%) | 0
---------+------------+------------+------
Total | 2 | 0 | 2
Grouped by e42dep (2)
c172code | male | female | NA | Total
---------+-----------+-----------+----------+------
2 | 2 (50.0%) | 2 (50.0%) | 0 (0.0%) | 4
<NA> | 0 (NaN%) | 0 (NaN%) | 0 (NaN%) | 0
---------+-----------+-----------+----------+------
Total | 2 | 2 | 0 | 4
c172code | male | female | NA | Total
---------+-----------+-----------+-----------+------
2 | 2 (50.0%) | 2 (50.0%) | 0 (0.0%) | 4
<NA> | 0 (NaN%) | 0 (NaN%) | 0 (NaN%) | 0
---------+-----------+-----------+-----------+------
Total | 2 | 2 | 0 | 4
Grouped by e42dep (3)
c172code | male | female | NA | Total
---------+-----------+------------+-----------+------
1 | 2 (50.0%) | 2 (50.0%) | 0 (0.0%) | 4
2 | 4 (25.0%) | 11 (68.8%) | 1 (6.2%) | 16
3 | 1 (16.7%) | 5 (83.3%) | 0 (0.0%) | 6
<NA> | 1 (50.0%) | 0 (0.0%) | 1 (50.0%) | 2
1 | 2 (50.0%) | 2 (50.0%) | 0 (0.0%) | 4
2 | 4 (25.0%) | 11 (68.8%) | 1 (6.2%) | 16
3 | 1 (16.7%) | 5 (83.3%) | 0 (0.0%) | 6
<NA> | 1 (50.0%) | 0 (0.0%) | 1 (50.0%) | 2
---------+-----------+------------+-----------+------
Total | 8 | 18 | 2 | 28
Grouped by e42dep (4)
c172code | male | female | NA | Total
---------+------------+------------+-----------+------
1 | 3 (75.0%) | 0 (0.0%) | 1 (25.0%) | 4
2 | 23 (54.8%) | 18 (42.9%) | 1 (2.4%) | 42
1 | 3 (75.0%) | 0 (0.0%) | 1 (25.0%) | 4
2 | 23 (54.8%) | 18 (42.9%) | 1 (2.4%) | 42
3 | 3 (30.0%) | 6 (60.0%) | 1 (10.0%) | 10
<NA> | 3 (42.9%) | 4 (57.1%) | 0 (0.0%) | 7
<NA> | 3 (42.9%) | 4 (57.1%) | 0 (0.0%) | 7
---------+------------+------------+-----------+------
Total | 32 | 28 | 3 | 63
Grouped by e42dep (NA)
c172code | male | female | NA | Total
---------+------------+------------+----------+------
2 | 0 (0.0%) | 2 (100.0%) | 0 (0.0%) | 2
<NA> | 1 (100.0%) | 0 (0.0%) | 0 (0.0%) | 1
---------+------------+------------+----------+------
Total | 1 | 2 | 0 | 3
c172code | male | female | NA | Total
---------+------------+------------+------------+------
2 | 0 (0.0%) | 2 (100.0%) | 0 (0.0%) | 2
<NA> | 1 (100.0%) | 0 (0.0%) | 0 (0.0%) | 1
---------+------------+------------+------------+------
Total | 1 | 2 | 0 | 3

# data_tabulate, cross tables, markdown
Expand All @@ -482,10 +482,10 @@
Output
[1] "|efc$c172code | male| female| NA | Total|"
[2] "|:------------|----------:|----------:|:--------|-----:|"
[3] "|1 | 5 (5.0%)| 2 (2.0%)|1 (1.0%) | 8|"
[3] "|1 | 5 (5.0%)| 2 (2.0%)|1 (1.0%) | 8|"
[4] "|2 | 31 (31.0%)| 33 (33.0%)|2 (2.0%) | 66|"
[5] "|3 | 4 (4.0%)| 11 (11.0%)|1 (1.0%) | 16|"
[6] "|<NA> | 5 (5.0%)| 4 (4.0%)|1 (1.0%) | 10|"
[5] "|3 | 4 (4.0%)| 11 (11.0%)|1 (1.0%) | 16|"
[6] "|<NA> | 5 (5.0%)| 4 (4.0%)|1 (1.0%) | 10|"
[7] "| | | | | |"
[8] "|Total | 45| 50| 5 | 100|"
attr(,"format")
Expand All @@ -501,9 +501,9 @@
Output
[1] "|efc$c172code | male| female| Total|"
[2] "|:------------|----------:|----------:|-----:|"
[3] "|1 | 5 (5.8%)| 2 (2.3%)| 7|"
[3] "|1 | 5 (5.8%)| 2 (2.3%)| 7|"
[4] "|2 | 31 (36.0%)| 33 (38.4%)| 64|"
[5] "|3 | 4 (4.7%)| 11 (12.8%)| 15|"
[5] "|3 | 4 (4.7%)| 11 (12.8%)| 15|"
[6] "| | | | |"
[7] "|Total | 40| 46| 86|"
attr(,"format")
Expand All @@ -519,10 +519,10 @@
Output
[1] "|efc$c172code | male| female| NA | Total|"
[2] "|:------------|----------:|----------:|:--------|-----:|"
[3] "|1 | 5 (4.8%)| 3 (2.9%)|2 (1.9%) | 10|"
[3] "|1 | 5 (4.8%)| 3 (2.9%)|2 (1.9%) | 10|"
[4] "|2 | 32 (30.5%)| 32 (30.5%)|3 (2.9%) | 67|"
[5] "|3 | 3 (2.9%)| 11 (10.5%)|1 (1.0%) | 15|"
[6] "|<NA> | 8 (7.6%)| 5 (4.8%)|1 (1.0%) | 14|"
[5] "|3 | 3 (2.9%)| 11 (10.5%)|1 (1.0%) | 15|"
[6] "|<NA> | 8 (7.6%)| 5 (4.8%)|1 (1.0%) | 14|"
[7] "| | | | | |"
[8] "|Total | 48| 51| 7 | 105|"
attr(,"format")
Expand All @@ -538,9 +538,9 @@
Output
[1] "|efc$c172code | male| female| Total|"
[2] "|:------------|----------:|----------:|-----:|"
[3] "|1 | 5 (5.8%)| 3 (3.5%)| 8|"
[3] "|1 | 5 (5.8%)| 3 (3.5%)| 8|"
[4] "|2 | 32 (37.2%)| 32 (37.2%)| 64|"
[5] "|3 | 3 (3.5%)| 11 (12.8%)| 14|"
[5] "|3 | 3 (3.5%)| 11 (12.8%)| 14|"
[6] "| | | | |"
[7] "|Total | 40| 46| 86|"
attr(,"format")
Expand Down

0 comments on commit 12eb590

Please sign in to comment.