Skip to content

Commit

Permalink
Suggestion of new function: describe_missing()
Browse files Browse the repository at this point in the history
Fixes #454
  • Loading branch information
rempsyc committed Nov 11, 2024
1 parent 218b7f4 commit ebaeb68
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 16 deletions.
8 changes: 4 additions & 4 deletions R/describe_missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
#' describe_missing(df, scales = c("ID", "open", "extrovert", "agreeable"))
describe_missing <- function(data, vars = NULL, scales = NULL) {
classes <- lapply(data, class)
if (missing(vars) & missing(scales)) {
if (missing(vars) && missing(scales)) {
vars.internal <- names(data)
} else if (!missing(scales)) {
vars.internal <- lapply(scales, function(x) {
Expand All @@ -81,7 +81,7 @@ describe_missing <- function(data, vars = NULL, scales = NULL) {
vars.internal <- list(vars.internal)
}
na_df <- .describe_missing(data)
if (!missing(vars) | !missing(scales)) {
if (!missing(vars) || !missing(scales)) {
na_list <- lapply(vars.internal, function(x) {
data_subset <- data[, x, drop = FALSE]
.describe_missing(data_subset)
Expand All @@ -93,7 +93,7 @@ describe_missing <- function(data, vars = NULL, scales = NULL) {
}

.describe_missing <- function(data) {
var <- paste0(names(data)[1], ":", names(data)[ncol(data)])
my_var <- paste0(names(data)[1], ":", names(data)[ncol(data)])
items <- ncol(data)
na <- sum(is.na(data))
cells <- nrow(data) * ncol(data)
Expand All @@ -103,7 +103,7 @@ describe_missing <- function(data, vars = NULL, scales = NULL) {
all_na <- sum(apply(data, 1, function(x) all(is.na(x))))

data.frame(
var = var,
var = my_var,
items = items,
na = na,
cells = cells,
Expand Down
7 changes: 0 additions & 7 deletions tests/testthat/_snaps/describe_missing.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@
2 Temp:Day 3 0 459 0.00 0 0.00 0
3 Total 6 44 918 4.79 2 33.33 0

---

Code
df <- data.frame(ID = c("idz", NA), scale1_Q1 = fun(), scale1_Q2 = fun(),
scale1_Q3 = fun(), scale2_Q1 = fun(), scale2_Q2 = fun(), scale2_Q3 = fun(),
scale3_Q1 = fun(), scale3_Q2 = fun(), scale3_Q3 = fun())

---

Code
Expand Down
11 changes: 6 additions & 5 deletions tests/testthat/test-describe_missing.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ test_that("describe_missing", {
fun <- function() {
c(sample(c(NA, 1:10), replace = TRUE), NA, NA, NA)
}
expect_snapshot(df <- data.frame(

# One can list the scale names directly:
df <- data.frame(
ID = c("idz", NA),
scale1_Q1 = fun(), scale1_Q2 = fun(), scale1_Q3 = fun(),
scale2_Q1 = fun(), scale2_Q2 = fun(), scale2_Q3 = fun(),
scale3_Q1 = fun(), scale3_Q2 = fun(), scale3_Q3 = fun()
))

# One can list the scale names directly:
scale3_Q1 = fun(), scale3_Q2 = fun(), scale3_Q3 = fun(),
stringsAsFactors = FALSE
)
expect_snapshot(describe_missing(df, scales = c("ID", "scale1", "scale2", "scale3")))
})

0 comments on commit ebaeb68

Please sign in to comment.