Skip to content

Commit ebaeb68

Browse files
committed
Suggestion of new function: describe_missing()
Fixes #454
1 parent 218b7f4 commit ebaeb68

File tree

3 files changed

+10
-16
lines changed

3 files changed

+10
-16
lines changed

R/describe_missing.R

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
#' describe_missing(df, scales = c("ID", "open", "extrovert", "agreeable"))
6868
describe_missing <- function(data, vars = NULL, scales = NULL) {
6969
classes <- lapply(data, class)
70-
if (missing(vars) & missing(scales)) {
70+
if (missing(vars) && missing(scales)) {
7171
vars.internal <- names(data)
7272
} else if (!missing(scales)) {
7373
vars.internal <- lapply(scales, function(x) {
@@ -81,7 +81,7 @@ describe_missing <- function(data, vars = NULL, scales = NULL) {
8181
vars.internal <- list(vars.internal)
8282
}
8383
na_df <- .describe_missing(data)
84-
if (!missing(vars) | !missing(scales)) {
84+
if (!missing(vars) || !missing(scales)) {
8585
na_list <- lapply(vars.internal, function(x) {
8686
data_subset <- data[, x, drop = FALSE]
8787
.describe_missing(data_subset)
@@ -93,7 +93,7 @@ describe_missing <- function(data, vars = NULL, scales = NULL) {
9393
}
9494

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

105105
data.frame(
106-
var = var,
106+
var = my_var,
107107
items = items,
108108
na = na,
109109
cells = cells,

tests/testthat/_snaps/describe_missing.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,6 @@
1717
2 Temp:Day 3 0 459 0.00 0 0.00 0
1818
3 Total 6 44 918 4.79 2 33.33 0
1919

20-
---
21-
22-
Code
23-
df <- data.frame(ID = c("idz", NA), scale1_Q1 = fun(), scale1_Q2 = fun(),
24-
scale1_Q3 = fun(), scale2_Q1 = fun(), scale2_Q2 = fun(), scale2_Q3 = fun(),
25-
scale3_Q1 = fun(), scale3_Q2 = fun(), scale3_Q3 = fun())
26-
2720
---
2821

2922
Code

tests/testthat/test-describe_missing.R

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,14 @@ test_that("describe_missing", {
1414
fun <- function() {
1515
c(sample(c(NA, 1:10), replace = TRUE), NA, NA, NA)
1616
}
17-
expect_snapshot(df <- data.frame(
17+
18+
# One can list the scale names directly:
19+
df <- data.frame(
1820
ID = c("idz", NA),
1921
scale1_Q1 = fun(), scale1_Q2 = fun(), scale1_Q3 = fun(),
2022
scale2_Q1 = fun(), scale2_Q2 = fun(), scale2_Q3 = fun(),
21-
scale3_Q1 = fun(), scale3_Q2 = fun(), scale3_Q3 = fun()
22-
))
23-
24-
# One can list the scale names directly:
23+
scale3_Q1 = fun(), scale3_Q2 = fun(), scale3_Q3 = fun(),
24+
stringsAsFactors = FALSE
25+
)
2526
expect_snapshot(describe_missing(df, scales = c("ID", "scale1", "scale2", "scale3")))
2627
})

0 commit comments

Comments
 (0)