Skip to content

Commit 9992db4

Browse files
minor bug in excluding factor
1 parent 1bf7677 commit 9992db4

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

R/datasummary_skim.R

+10-11
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,12 @@ datasummary_skim_categorical <- function(
284284

285285
for (n in colnames(dat_new)) {
286286

287+
# completely missing
288+
if (all(is.na(dat_new[[n]]))) {
289+
dat_new[[n]] <- NULL
290+
drop_entirely_na <- c(drop_entirely_na, n)
291+
}
292+
287293
if (is.logical(dat_new[[n]]) |
288294
is.character(dat_new[[n]]) |
289295
is.factor(dat_new[[n]])) {
@@ -299,21 +305,14 @@ datasummary_skim_categorical <- function(
299305
levels(dat_new[[n]])[idx] <- " "
300306
}
301307

302-
# completely missing
303-
if (all(is.na(dat_new[[n]]))) {
304-
dat_new[[n]] <- NULL
305-
drop_entirely_na <- c(drop_entirely_na, n)
306-
} else {
307-
# factors with too many levels
308-
if (is.factor(dat_new[[n]])) {
308+
## factors with too many levels
309+
if (is.factor(dat_new[[n]])) {
309310
if (length(levels(dat_new[[n]])) > 50) {
310-
dat_new[[n]] <- NULL
311-
drop_too_many_levels <- c(drop_too_many_levels, n)
311+
dat_new[[n]] <- NULL
312+
drop_too_many_levels <- c(drop_too_many_levels, n)
312313
}
313-
}
314314
}
315315

316-
317316
# discard non-factors
318317
} else {
319318
dat_new[[n]] <- NULL

tests/testthat/test-datasummary_skim.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ test_that("too many factor levels", {
108108

109109
test_that("completely missing variables are dropped", {
110110
tmp <- dat
111-
tmp$junk <- rep(NA, nrow(dat))
111+
tmp$junk <- rep(NA_character_, nrow(dat))
112112
tmp <- expect_warning(datasummary_skim(tmp, type="categorical", output="data.frame", histogram=FALSE))
113113
expect_false("junk" %in% tmp[[1]])
114114
})

0 commit comments

Comments
 (0)