Skip to content

Commit

Permalink
add has_sparse_elements
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilHvitfeldt committed Sep 17, 2024
1 parent 782bdbd commit 031eda1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(as_sparse_logical)
export(coerce_to_sparse_data_frame)
export(coerce_to_sparse_matrix)
export(coerce_to_sparse_tibble)
export(has_sparse_elements)
export(is_sparse_character)
export(is_sparse_double)
export(is_sparse_integer)
Expand Down
40 changes: 40 additions & 0 deletions R/has_sparse_elements.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' Check for sparse elements
#'
#' This function checks to see if a data.frame, tibble or list contains one or
#' more sparse vectors.
#'
#' @param x a data frame, tibble, or list.
#'
#' @details
#' The checking in this function is done using [is_sparse_vector()], but is
#' implemented using an early exit pattern to provide fast performance for wide
#' data.frames.
#'
#' This function does not test whether `x` is a data.frame, tibble or list. It
#' simply iterates over the elements and sees if they are sparse vectors.
#'
#' @return A single logical value.
#'
#' @examplesIf rlang::is_installed("Matrix")
#' set.seed(1234)
#' n_cols <- 10000
#' mat <- matrix(sample(0:1, n_cols * 10, TRUE, c(0.9, 0.1)), ncol = n_cols)
#' colnames(mat) <- as.character(seq_len(n_cols))
#' sparse_mat <- Matrix::Matrix(mat, sparse = TRUE)
#'
#' res <- coerce_to_sparse_tibble(sparse_mat)
#' has_sparse_elements(res)
#'
#' has_sparse_elements(mtcars)
#' @export
has_sparse_elements <- function(x) {
res <- FALSE

for (elt in x) {
if (is_sparse_vector(elt)) {
res <- TRUE
break
}
}
res
}
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ reference:
contents:
- type-predicates
- extractors
- has_sparse_elements
40 changes: 40 additions & 0 deletions man/has_sparse_elements.Rd

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

0 comments on commit 031eda1

Please sign in to comment.