From 75c1413bd74b9665a53edad587b7b2c22b2079d0 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 4 Mar 2024 15:05:01 +0100 Subject: [PATCH] address comments --- R/data_summary.R | 13 +++++++++---- man/data_summary.Rd | 2 +- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/R/data_summary.R b/R/data_summary.R index bddd360ca..d604b2fb5 100644 --- a/R/data_summary.R +++ b/R/data_summary.R @@ -8,7 +8,7 @@ #' @param by Optional character string, indicating the name of a variable in `x`. #' If supplied, the data will be split by this variable and summary statistics #' will be computed for each group. -#' @param include_na Logical, if `TRUE`, missing values are included as a level +#' @param include_na Logical. If `TRUE`, missing values are included as a level #' in the grouping variable. If `FALSE`, missing values are omitted from the #' grouping variable. #' @param ... One or more named expressions that define the new variable name @@ -88,19 +88,24 @@ data_summary.data.frame <- function(x, ..., by = NULL, include_na = TRUE) { } else { # sanity check - is "by" a character string? if (!is.character(by)) { - insight::format_error("Argument `by` must be a character string, indicating the name of a variable in the data.") + insight::format_error("Argument `by` must be a character string indicating the name of variables in the data.") } # is "by" in the data? if (!all(by %in% colnames(x))) { by_not_found <- by[!by %in% colnames(x)] insight::format_error( - paste0("Variable \"", by_not_found, "\" not found in the data."), + paste0( + "Variable", + ifelse(length(by_not_found) > 1, "s ", " "), + text_concatenate(by_not_found, enclose = "\""), + " not found in the data." + ), .misspelled_string(colnames(x), by_not_found, "Possibly misspelled?") ) } # split data, add NA levels, if requested l <- lapply(x[by], function(i) { - if (include_na) { + if (include_na && anyNA(i)) { addNA(i) } else { i diff --git a/man/data_summary.Rd b/man/data_summary.Rd index ba3e6cd46..2c4408618 100644 --- a/man/data_summary.Rd +++ b/man/data_summary.Rd @@ -22,7 +22,7 @@ summary function \code{n()} can be used to count the number of observations.} If supplied, the data will be split by this variable and summary statistics will be computed for each group.} -\item{include_na}{Logical, if \code{TRUE}, missing values are included as a level +\item{include_na}{Logical. If \code{TRUE}, missing values are included as a level in the grouping variable. If \code{FALSE}, missing values are omitted from the grouping variable.} }