3939# ' printed as markdown or HTML table, depending on the environment. See
4040# ' [`insight::export_table()`] for details.
4141# ' @param verbose Toggle warnings and messages.
42+ # ' @param metrics Character vector, indicating the types of metrics to be
43+ # ' included. Only applies to frequencies, i.e. when `by` is `NULL`. Can include
44+ # ' any combination of `N` (frequencies including `NA`), `"raw"` (percentage
45+ # ' including `NA` values), `"valid"` (percentage excluding `NA` values) and
46+ # ' `"cumulative"` (percentage excluding `NA` values).
4247# ' @param ... not used.
4348# ' @inheritParams extract_column_names
4449# '
8691# ' # drop missing values
8792# ' data_tabulate(efc$c172code, remove_na = TRUE)
8893# '
94+ # ' # exclude the cumulative percent column
95+ # ' data_tabulate(efc$c172code, metrics = c("raw", "valid"))
96+ # '
8997# ' # data frame
9098# ' data_tabulate(efc, c("e42dep", "c172code"))
9199# '
@@ -154,6 +162,7 @@ data_tabulate.default <- function(
154162 proportions = NULL ,
155163 name = NULL ,
156164 verbose = TRUE ,
165+ metrics = c(" N" , " raw" , " valid" , " cumulative" ),
157166 ...
158167) {
159168 # save label attribute, before it gets lost...
@@ -260,7 +269,21 @@ data_tabulate.default <- function(
260269 out $ N <- round(out $ N )
261270 }
262271
263- out $ `Raw %` <- 100 * out $ N / sum(out $ N )
272+ # validate "metrics"
273+ if (! is.null(metrics )) {
274+ # match.arg(all.ok = "all") is only valid for R > 4.6, and
275+ # match.arg(all.ok = TRUE) doesn't error if some values are wrong.
276+ invalid <- setdiff(metrics , c(" N" , " raw" , " valid" , " cumulative" ))
277+ if (length(invalid ) > 0 ) {
278+ insight :: format_error(
279+ " Invalid values in `metrics`: " ,
280+ text_concatenate(invalid , enclose = ' "' )
281+ )
282+ }
283+ }
284+ if (" raw" %in% metrics ) {
285+ out $ `Raw %` <- 100 * out $ N / sum(out $ N )
286+ }
264287 # if we have missing values, we add a row with NA
265288 if (remove_na ) {
266289 out $ `Valid %` <- 100 * out $ N / sum(out $ N )
@@ -269,8 +292,13 @@ data_tabulate.default <- function(
269292 out $ `Valid %` <- c(100 * out $ N [- nrow(out )] / sum(out $ N [- nrow(out )]), NA )
270293 valid_n <- sum(out $ N [- length(out $ N )], na.rm = TRUE )
271294 }
272- out $ `Cumulative %` <- cumsum(out $ `Valid %` )
273295
296+ if (" cumulative" %in% metrics ) {
297+ out $ `Cumulative %` <- cumsum(out $ `Valid %` )
298+ }
299+ if (! " valid" %in% metrics ) {
300+ out $ `Valid %` <- NULL
301+ }
274302 # add information about variable/group names
275303 if (! is.null(obj_name )) {
276304 if (is.null(group_variable )) {
@@ -286,6 +314,10 @@ data_tabulate.default <- function(
286314 }
287315 out <- cbind(var_info , out )
288316 }
317+ total_n <- sum(out $ N , na.rm = TRUE )
318+ if (! " N" %in% metrics ) {
319+ out <- data_select(out , exclude = " N" )
320+ }
289321
290322 # save information
291323 attr(out , " type" ) <- .variable_type(x )
@@ -296,7 +328,7 @@ data_tabulate.default <- function(
296328 attr(out , " duplicate_varnames" ) <- duplicated(out $ Variable )
297329 attr(out , " weights" ) <- weights
298330
299- attr(out , " total_n" ) <- sum( out $ N , na.rm = TRUE )
331+ attr(out , " total_n" ) <- total_n
300332 attr(out , " valid_n" ) <- valid_n
301333 if (is.null(by )) {
302334 attr(out , " by" ) <- NULL
@@ -344,10 +376,6 @@ data_tabulate.data.frame <- function(
344376 verbose = verbose
345377 )
346378
347- if (! is.null(by )) {
348- attr(x , " by" ) <- by_name
349- }
350-
351379 # validate "by"
352380 by <- .validate_by(by , x )
353381 # validate "weights"
@@ -875,19 +903,21 @@ format.datawizard_table <- function(x, format = "text", big_mark = NULL, ...) {
875903 # convert to character manually, else, for large numbers,
876904 # format_table() returns scientific notation
877905 x <- as.data.frame(x )
878- x $ N <- as.character(x $ N )
879-
906+ if (! is.null(x $ N )) {
907+ x $ N <- as.character(x $ N )
908+ }
880909 # format data frame
881910 ftab <- insight :: format_table(x , ... )
882911 ftab [] <- lapply(ftab , function (i ) {
883912 i [i == " " ] <- ifelse(identical(format , " text" ), " <NA>" , " (NA)" ) # nolint
884913 i
885914 })
886- ftab $ N <- gsub(" \\ .00$" , " " , ftab $ N )
887-
888- # insert big marks?
889- ftab $ N <- .add_commas_in_numbers(ftab $ N , big_mark )
915+ if (! is.null(ftab $ N )) {
916+ ftab $ N <- gsub(" \\ .00$" , " " , ftab $ N )
890917
918+ # insert big marks?
919+ ftab $ N <- .add_commas_in_numbers(ftab $ N , big_mark )
920+ }
891921 ftab
892922}
893923
0 commit comments