Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

complete.cases() -> vec_detect_complete() #1365

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,6 @@ importFrom(purrr,map_dbl)
importFrom(purrr,map_lgl)
importFrom(stats,as.formula)
importFrom(stats,binomial)
importFrom(stats,complete.cases)
importFrom(stats,cov)
importFrom(stats,cov.wt)
importFrom(stats,lm)
Expand Down Expand Up @@ -794,4 +793,5 @@ importFrom(utils,install.packages)
importFrom(utils,object.size)
importFrom(vctrs,vec_cast)
importFrom(vctrs,vec_cbind)
importFrom(vctrs,vec_detect_complete)
importFrom(vctrs,vec_slice)
2 changes: 1 addition & 1 deletion R/case_weights.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ wt_calcs <- function(x, wts, statistic = "mean") {
wts <- rep(1L, nrow(x))
}

complete <- stats::complete.cases(x) & !is.na(wts)
complete <- vec_detect_complete(x) & !is.na(wts)
wts <- wts[complete]
x <- x[complete,,drop = FALSE]
res <- stats::cov.wt(x, wt = wts, cor = statistic == "cor")
Expand Down
2 changes: 1 addition & 1 deletion R/classdist.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#'
#' @examplesIf rlang::is_installed(c("modeldata"))
#' data(penguins, package = "modeldata")
#' penguins <- penguins[complete.cases(penguins), ]
#' penguins <- penguins[vctrs::vec_detect_complete(penguins), ]
#' penguins$island <- NULL
#' penguins$sex <- NULL
#'
Expand Down
2 changes: 1 addition & 1 deletion R/classdist_shrunken.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
#' of the National Academy of Sciences_, 99(10), 6567-6572.
#' @examplesIf rlang::is_installed(c("modeldata"))
#' data(penguins, package = "modeldata")
#' penguins <- penguins[complete.cases(penguins), ]
#' penguins <- penguins[vctrs::vec_detect_complete(penguins), ]
#' penguins$island <- NULL
#' penguins$sex <- NULL
#'
Expand Down
2 changes: 1 addition & 1 deletion R/corr.R
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ corr_filter <-
method = "pearson") {
x <- correlations(x, wts = wts, use = use, method = method)

if (any(!complete.cases(x))) {
if (any(!vec_detect_complete(x))) {
all_na <- apply(x, 2, function(x) all(is.na(x)))
if (sum(all_na) >= nrow(x) - 1) {
cli::cli_warn(
Expand Down
4 changes: 2 additions & 2 deletions R/impute_bag.R
Original file line number Diff line number Diff line change
Expand Up @@ -248,14 +248,14 @@ bake.step_impute_bag <- function(object, new_data, ...) {
col_names <- names(object$models)
check_new_data(col_names, object, new_data)

missing_rows <- !complete.cases(new_data)
missing_rows <- !vec_detect_complete(new_data)
if (!any(missing_rows)) {
return(new_data)
}

old_data <- new_data
for (col_name in col_names) {
missing_rows <- !complete.cases(new_data[[col_name]])
missing_rows <- !vec_detect_complete(new_data[[col_name]])
if (!any(missing_rows)) {
next
}
Expand Down
4 changes: 2 additions & 2 deletions R/impute_knn.R
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ bake.step_impute_knn <- function(object, new_data, ...) {
all_cols <- unique(unlist(object$columns, recursive = TRUE))
check_new_data(all_cols, object, new_data)

missing_rows <- !complete.cases(new_data)
missing_rows <- !vec_detect_complete(new_data)
if (!any(missing_rows)) {
return(new_data)
}
Expand All @@ -230,7 +230,7 @@ bake.step_impute_knn <- function(object, new_data, ...) {

old_data <- new_data
for (col_name in col_names) {
missing_rows <- !complete.cases(new_data[, col_name])
missing_rows <- !vec_detect_complete(new_data[, col_name])
if (!any(missing_rows)) {
next
}
Expand Down
6 changes: 3 additions & 3 deletions R/impute_linear.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ step_impute_linear_new <-

lm_wrap <- function(vars, dat, wts = NULL, call = caller_env(2)) {
dat <- as.data.frame(dat[, c(vars$y, vars$x)])
complete <- stats::complete.cases(dat)
complete <- vec_detect_complete(dat)
dat <- dat[complete, ]
wts <- wts[complete]
if (nrow(dat) == 0) {
Expand Down Expand Up @@ -200,14 +200,14 @@ bake.step_impute_linear <- function(object, new_data, ...) {
col_names <- names(object$models)
check_new_data(col_names, object, new_data)

missing_rows <- !complete.cases(new_data)
missing_rows <- !vec_detect_complete(new_data)
if (!any(missing_rows)) {
return(new_data)
}

old_data <- new_data
for (col_name in col_names) {
missing_rows <- !complete.cases(new_data[[col_name]])
missing_rows <- !vec_detect_complete(new_data[[col_name]])
if (!any(missing_rows)) {
next
}
Expand Down
4 changes: 2 additions & 2 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ strings2factors <- function(x, info) {

# ------------------------------------------------------------------------------

# `complete.cases` fails on list columns. This version counts a list column
# `vec_detect_complete` fails on list columns. This version counts a list column
# as missing if _all_ values are missing. For if a list vector element is a
# data frame with one missing value, that element of the list column will
# be counted as complete.
Expand All @@ -218,7 +218,7 @@ n_complete_rows <- function(x) {
x[[pos_list_col]] <- purrr::map_lgl(x[[pos_list_col]], flatten_na)
}

sum(complete.cases(x))
sum(vec_detect_complete(x))
}

flatten_na <- function(x) {
Expand Down
2 changes: 1 addition & 1 deletion R/recipes-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
#' @importFrom purrr map_lgl
#' @importFrom stats as.formula
#' @importFrom stats binomial
#' @importFrom stats complete.cases
#' @importFrom stats cov
#' @importFrom stats cov.wt
#' @importFrom stats lm
Expand Down Expand Up @@ -99,6 +98,7 @@
#' @importFrom utils object.size
#' @importFrom vctrs vec_cast
#' @importFrom vctrs vec_cbind
#' @importFrom vctrs vec_detect_complete
#' @importFrom vctrs vec_slice
## usethis namespace: end
NULL
2 changes: 1 addition & 1 deletion man/step_classdist.Rd

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

2 changes: 1 addition & 1 deletion man/step_classdist_shrunken.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test-dummy.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ sacr$city[sample(1:nrow(sacr), 20)] <- NA_character_
sacr_missing <- sacr

sacr$city[is.na(sacr$city)] <- "missing"
sacr <- sacr[complete.cases(sacr), -3]
sacr <- sacr[vec_detect_complete(sacr), -3]

sacr_fac <- sacr
sacr_fac$city <- factor(sacr_fac$city)
Expand Down Expand Up @@ -167,10 +167,10 @@ test_that("tests for NA values in factor", {
)

expect_true(
all(complete.cases(factors_data_0) == complete.cases(sacr_missing[, "city"]))
all(vec_detect_complete(factors_data_0) == vec_detect_complete(sacr_missing[, "city"]))
)
expect_true(
all(complete.cases(factors_data_1) == complete.cases(sacr_missing[, "city"]))
all(vec_detect_complete(factors_data_1) == vec_detect_complete(sacr_missing[, "city"]))
)
})

Expand All @@ -189,10 +189,10 @@ test_that("tests for NA values in ordered factor", {
)

expect_true(
all(complete.cases(factors_data_0) == complete.cases(sacr_ordered[, "city"]))
all(vec_detect_complete(factors_data_0) == vec_detect_complete(sacr_ordered[, "city"]))
)
expect_true(
all(complete.cases(factors_data_1) == complete.cases(sacr_ordered[, "city"]))
all(vec_detect_complete(factors_data_1) == vec_detect_complete(sacr_ordered[, "city"]))
)
})

Expand Down
Loading