From 12eb590b07005ebb01a2f6f7896250dc18797fdd Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Feb 2024 15:30:12 +0100 Subject: [PATCH] align values in tables --- R/data_xtabulate.R | 24 ++--- tests/testthat/_snaps/data_tabulate.md | 118 ++++++++++++------------- 2 files changed, 68 insertions(+), 74 deletions(-) diff --git a/R/data_xtabulate.R b/R/data_xtabulate.R index ff259015f..dd286fed3 100644 --- a/R/data_xtabulate.R +++ b/R/data_xtabulate.R @@ -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") ) } } diff --git a/tests/testthat/_snaps/data_tabulate.md b/tests/testthat/_snaps/data_tabulate.md index a7af67dbf..77c242115 100644 --- a/tests/testthat/_snaps/data_tabulate.md +++ b/tests/testthat/_snaps/data_tabulate.md @@ -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 - | 5 (5.0%) | 4 (4.0%) | 1 (1.0%) | 10 + 3 | 4 (4.0%) | 11 (11.0%) | 1 (1.0%) | 16 + | 5 (5.0%) | 4 (4.0%) | 1 (1.0%) | 10 -------------+------------+------------+----------+------ Total | 45 | 50 | 5 | 100 @@ -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 @@ -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 - | 8 (7.6%) | 5 (4.8%) | 1 (1.0%) | 14 + 3 | 3 (2.9%) | 11 (10.5%) | 1 (1.0%) | 15 + | 8 (7.6%) | 5 (4.8%) | 1 (1.0%) | 14 -------------+------------+------------+----------+------ Total | 48 | 51 | 7 | 105 @@ -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 @@ -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 | 5 (50.0%) | 4 (40.0%) | 1 (10.0%) | 10 ---------+------------+------------+-----------+------ Total | 45 | 50 | 5 | 100 @@ -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 - | 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 + | 8 (57.1%) | 5 (35.7%) | 1 (7.1%) | 14 ---------+------------+------------+-----------+------ Total | 48 | 51 | 7 | 105 @@ -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 - | 5 (11.1%) | 4 (8.0%) | 1 (20.0%) | 10 + 3 | 4 (8.9%) | 11 (22.0%) | 1 (20.0%) | 16 + | 5 (11.1%) | 4 (8.0%) | 1 (20.0%) | 10 ---------+------------+------------+-----------+------ Total | 45 | 50 | 5 | 100 @@ -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 ---------+------------+------------+------ @@ -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 - | 8 (16.7%) | 5 (9.8%) | 1 (14.3%) | 14 + 3 | 3 (6.2%) | 11 (21.6%) | 1 (14.3%) | 15 + | 8 (16.7%) | 5 (9.8%) | 1 (14.3%) | 14 ---------+------------+------------+-----------+------ Total | 48 | 51 | 7 | 105 @@ -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 @@ -427,30 +427,30 @@ Output Grouped by e42dep (1) - c172code | male | NA | Total - ---------+------------+----------+------ - 2 | 2 (100.0%) | 0 (0.0%) | 2 - | 0 (NaN%) | 0 (NaN%) | 0 - ---------+------------+----------+------ - Total | 2 | 0 | 2 + c172code | male | NA | Total + ---------+------------+------------+------ + 2 | 2 (100.0%) | 0 (0.0%) | 2 + | 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 - | 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 + | 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 - | 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 + | 1 (50.0%) | 0 (0.0%) | 1 (50.0%) | 2 ---------+-----------+------------+-----------+------ Total | 8 | 18 | 2 | 28 @@ -458,21 +458,21 @@ 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 - | 3 (42.9%) | 4 (57.1%) | 0 (0.0%) | 7 + | 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 - | 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 + | 1 (100.0%) | 0 (0.0%) | 0 (0.0%) | 1 + ---------+------------+------------+------------+------ + Total | 1 | 2 | 0 | 3 # data_tabulate, cross tables, markdown @@ -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] "| | 5 (5.0%)| 4 (4.0%)|1 (1.0%) | 10|" + [5] "|3 | 4 (4.0%)| 11 (11.0%)|1 (1.0%) | 16|" + [6] "| | 5 (5.0%)| 4 (4.0%)|1 (1.0%) | 10|" [7] "| | | | | |" [8] "|Total | 45| 50| 5 | 100|" attr(,"format") @@ -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") @@ -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] "| | 8 (7.6%)| 5 (4.8%)|1 (1.0%) | 14|" + [5] "|3 | 3 (2.9%)| 11 (10.5%)|1 (1.0%) | 15|" + [6] "| | 8 (7.6%)| 5 (4.8%)|1 (1.0%) | 14|" [7] "| | | | | |" [8] "|Total | 48| 51| 7 | 105|" attr(,"format") @@ -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")