From 06146e5099b16e0c947beda1a5a4c724c64f8c58 Mon Sep 17 00:00:00 2001 From: Daniel Date: Tue, 13 Feb 2024 15:15:10 +0100 Subject: [PATCH] lintr --- R/data_tabulate.R | 43 +++++++++++++++++++------------------- R/data_xtabulate.R | 51 +++++++++++++++++++++++----------------------- 2 files changed, 46 insertions(+), 48 deletions(-) diff --git a/R/data_tabulate.R b/R/data_tabulate.R index 02f0effff..b861c171b 100644 --- a/R/data_tabulate.R +++ b/R/data_tabulate.R @@ -149,29 +149,28 @@ data_tabulate.default <- function(x, } else { freq_table <- tryCatch(table(x), error = function(e) NULL) } + } else if (include_na) { + # weighted frequency table, including NA + freq_table <- tryCatch( + stats::xtabs( + weights ~ x, + data = data.frame(weights = weights, x = addNA(x)), + na.action = stats::na.pass, + addNA = TRUE + ), + error = function(e) NULL + ) } else { - # weighted frequency table - if (include_na) { - freq_table <- tryCatch( - stats::xtabs( - weights ~ x, - data = data.frame(weights = weights, x = addNA(x)), - na.action = stats::na.pass, - addNA = TRUE - ), - error = function(e) NULL - ) - } else { - freq_table <- tryCatch( - stats::xtabs( - weights ~ x, - data = data.frame(weights = weights, x = x), - na.action = stats::na.omit, - addNA = FALSE - ), - error = function(e) NULL - ) - } + # weighted frequency table, excluding NA + freq_table <- tryCatch( + stats::xtabs( + weights ~ x, + data = data.frame(weights = weights, x = x), + na.action = stats::na.omit, + addNA = FALSE + ), + error = function(e) NULL + ) } if (is.null(freq_table)) { diff --git a/R/data_xtabulate.R b/R/data_xtabulate.R index fec2ad2d3..ff259015f 100644 --- a/R/data_xtabulate.R +++ b/R/data_xtabulate.R @@ -17,29 +17,28 @@ } else { x_table <- tryCatch(table(x, by), error = function(e) NULL) } + } else if (include_na) { + # weighted frequency table, including NA + x_table <- tryCatch( + stats::xtabs( + weights ~ x + by, + data = data.frame(weights = weights, x = addNA(x), by = addNA(by)), + na.action = stats::na.pass, + addNA = TRUE + ), + error = function(e) NULL + ) } else { - # weighted frequency table - if (include_na) { - x_table <- tryCatch( - stats::xtabs( - weights ~ x + by, - data = data.frame(weights = weights, x = addNA(x), by = addNA(by)), - na.action = stats::na.pass, - addNA = TRUE - ), - error = function(e) NULL - ) - } else { - x_table <- tryCatch( - stats::xtabs( - weights ~ x + by, - data = data.frame(weights = weights, x = x, by = by), - na.action = stats::na.omit, - addNA = FALSE - ), - error = function(e) NULL - ) - } + # weighted frequency table, excluding NA + x_table <- tryCatch( + stats::xtabs( + weights ~ x + by, + data = data.frame(weights = weights, x = x, by = by), + na.action = stats::na.omit, + addNA = FALSE + ), + error = function(e) NULL + ) } if (is.null(x_table)) { @@ -152,19 +151,19 @@ format.dw_data_xtabulate <- function(x, format = "text", digits = 1, big_mark = ftab$Total <- gsub("\\.00$", "", as.character(total_column)) # for text format, insert "empty row" before last total row if (identical(format, "text") || identical(format, "markdown")) { - sub <- as.data.frame(t(data.frame( + empty_row <- as.data.frame(t(data.frame( rep("", ncol(ftab)), c("Total", as.character(total_row)), stringsAsFactors = FALSE ))) } else { - sub <- as.data.frame(t(data.frame( + empty_row <- as.data.frame(t(data.frame( c("Total", as.character(total_row)), stringsAsFactors = FALSE ))) } - colnames(sub) <- colnames(ftab) - ftab <- rbind(ftab, sub) + colnames(empty_row) <- colnames(ftab) + ftab <- rbind(ftab, empty_row) ftab[nrow(ftab), ] <- gsub("\\.00$", "", ftab[nrow(ftab), ]) # insert big marks?