diff --git a/R/describe_missing.R b/R/describe_missing.R index 8d215e366..620d14da1 100644 --- a/R/describe_missing.R +++ b/R/describe_missing.R @@ -38,12 +38,14 @@ #' df, #' select = -1, #' names_sep = "_", -#' names_to = c("dimension", "item")) +#' names_to = c("dimension", "item") +#' ) #' #' describe_missing( #' df_long, #' select = -c(1, 3), -#' by = "dimension") +#' by = "dimension" +#' ) #' describe_missing <- function(data, select = NULL, @@ -65,9 +67,14 @@ describe_missing <- function(data, ... ) } - if (!is.null(by)) { + if (is.null(by)) { + na_list <- lapply(names(data), function(x) { + data_subset <- data[, x, drop = FALSE] + .describe_missing(data_subset) + }) + } else { if (!by %in% names(data)) { - stop("The 'by' column does not exist in the data.") + stop("The 'by' column does not exist in the data.", call. = FALSE) } grouped_data <- split(data, data[[by]]) na_list <- lapply(names(grouped_data), function(group_name) { @@ -82,11 +89,6 @@ describe_missing <- function(data, group_na_df$variable <- group_name group_na_df }) - } else { - na_list <- lapply(names(data), function(x) { - data_subset <- data[, x, drop = FALSE] - .describe_missing(data_subset) - }) } na_df <- do.call(rbind, na_list) if (isTRUE(sort)) { diff --git a/man/describe_missing.Rd b/man/describe_missing.Rd index bda13d11c..daf863738 100644 --- a/man/describe_missing.Rd +++ b/man/describe_missing.Rd @@ -120,11 +120,13 @@ df_long <- reshape_longer( df, select = -1, names_sep = "_", - names_to = c("dimension", "item")) + names_to = c("dimension", "item") +) describe_missing( df_long, select = -c(1, 3), - by = "dimension") + by = "dimension" +) } diff --git a/tests/testthat/test-describe_missing.R b/tests/testthat/test-describe_missing.R index 9c3dba43d..d26758cca 100644 --- a/tests/testthat/test-describe_missing.R +++ b/tests/testthat/test-describe_missing.R @@ -24,7 +24,8 @@ test_that("describe_missing", { ID = c("idz", NA), openness_1 = fun(), openness_2 = fun(), openness_3 = fun(), extroversion_1 = fun(), extroversion_2 = fun(), extroversion_3 = fun(), - agreeableness_1 = fun(), agreeableness_2 = fun(), agreeableness_3 = fun() + agreeableness_1 = fun(), agreeableness_2 = fun(), agreeableness_3 = fun(), + stringsAsFactors = FALSE ) # Pivot and group using datawizard