Skip to content

Commit

Permalink
add digit argument to miss_var_summary to help display %missing d…
Browse files Browse the repository at this point in the history
…ata correctly when there is a very small fraction of missingness. resolves #284
  • Loading branch information
njtierney committed Apr 10, 2023
1 parent 1a1efce commit 0ae7c9a
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## New

- implement `impute_fixed`, `impute_zero`, and `impute_factor`. notably these do not implement "scoped variants" which were previously implemented - for example, `impute_fixed_if` etc. This is in favour of using the new `across` workflow within `dplyr`, and it is easier to maintain. #261
- add `digit` argument to `miss_var_summary` to help display %missing data correctly when there is a very small fraction of missingness. #284

# naniar 1.0.0

Expand Down
16 changes: 13 additions & 3 deletions R/miss-x-summary.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#' sum of missings to the data. This can be useful when exploring patterns
#' of nonresponse. These are calculated as the cumulative sum of the missings
#' in the variables as they are first presented to the function.
#' @param digits how many digits to display in `pct_miss` column. Useful when
#' you are working with small amounts of missing data.
#' @param ... extra arguments
#'
#' @note `n_miss_cumsum` is calculated as the cumulative sum of missings in the
Expand Down Expand Up @@ -39,6 +41,7 @@
miss_var_summary <- function(data,
order = FALSE,
add_cumsum = FALSE,
digits,
...) {

test_if_null(data)
Expand All @@ -52,14 +55,19 @@ miss_var_summary <- function(data,
miss_var_summary.default <- function(data,
order = TRUE,
add_cumsum = FALSE,
digits = NULL,
...) {

col_n_miss <- colSums(is.na(data))
col_pct_miss <- colMeans(is.na(data)) * 100
col_pct_miss <- as.numeric(colMeans(is.na(data)) * 100)

res <- tibble::tibble(variable = names(col_n_miss),
n_miss = as.integer(col_n_miss),
pct_miss = as.numeric(col_pct_miss))
pct_miss = tibble::num(
x = col_pct_miss,
# control how rounding is presented
digits = digits)
)

if (add_cumsum) {
res <- res %>% dplyr::mutate(n_miss_cumsum = cumsum(n_miss))
Expand All @@ -77,12 +85,14 @@ miss_var_summary.default <- function(data,
miss_var_summary.grouped_df <- function(data,
order = TRUE,
add_cumsum = FALSE,
digits = NULL,
...) {

group_by_fun(data,
.fun = miss_var_summary,
order = order,
add_cumsum = add_cumsum)
add_cumsum = add_cumsum,
digits = digits)

}

Expand Down
5 changes: 4 additions & 1 deletion man/miss_var_summary.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0ae7c9a

Please sign in to comment.