Skip to content

Commit

Permalink
data_tabulate() for crosstables prints NA-proportions when no value…
Browse files Browse the repository at this point in the history
…s are available

Fixes #492
  • Loading branch information
strengejacke committed Mar 25, 2024
1 parent 9c2deb7 commit 46ea1a5
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions R/data_xtabulate.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +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] <- paste(
format(x[i, -1]),
format(sprintf("(%.*f%%)", digits, 100 * x[i, -1] / sum(x[i, -1], na.rm = TRUE)), justify = "right")
)
row_sum <- sum(x[i, -1], na.rm = TRUE)
if (row_sum == 0) {
row_sum_string <- "(0%)"
} else {
row_sum_string <- sprintf("(%.*f%%)", digits, 100 * x[i, -1] / row_sum)
}
tmp[i, -1] <- paste(format(x[i, -1]), format(row_sum_string, justify = "right"))
}
} else if (identical(props, "column")) {
for (i in seq_len(ncol(x))[-1]) {
tmp[, i] <- paste(
format(x[, i]),
format(sprintf("(%.*f%%)", digits, 100 * x[, i] / sum(x[, i], na.rm = TRUE)), justify = "right")
)
row_sum <- sum(x[, i], na.rm = TRUE)
if (row_sum == 0) {
row_sum_string <- "(0%)"
} else {
row_sum_string <- sprintf("(%.*f%%)", digits, 100 * x[, i] / row_sum)
}
tmp[, i] <- paste(format(x[, i]), format(row_sum_string, justify = "right"))
}
} else if (identical(props, "full")) {
for (i in seq_len(ncol(x))[-1]) {
Expand Down

0 comments on commit 46ea1a5

Please sign in to comment.