R/filter-combine.R

Defines functions filter_combine

Documented in filter_combine

#' Filter combine
#'
#' Filters a data frame then binds the result with it (with names).
#'
#' @param x A data.frame
#' @inheritDotParams dplyr::filter -.data -.preserve
#' @param .id When .id is supplied, a new column of identifiers is created to link each row to its original data frame.
#' The labels are taken from `.names`
#' @param .names vector of characters for the
#'
#' @export
#'
#' @seealso [dplyr::filter()] and [dplyr::bind_rows()]

filter_combine <- function(x, ..., .id, .names) {
  require_namespace("rlang")
  require_namespace("dplyr")
  stopifnot(length(.names) <= 2)
  dots <- rlang::enquos(...)
  xy <- list(x, dplyr::filter(x, !!!dots))
  names(xy) <- .names
  dplyr::bind_rows(xy, .id = .id)
}
jmbarbone/jordanExtra documentation built on Aug. 4, 2023, 5:37 p.m.