#'Sift thru dataset
#'
#'perform augmented filtering,
#'selectively including neighboring rows in addition to key observations.
#'
#' @param .data dataset to sift
#' @param condition Filter Condition (same expression you would use in oridinary dplyr filter)
#' @param variable Scoping variable
#' @param amount Scoping amount
#' @param constant Variable to hold fixed for each chunk
#' @param bind_rows If FALSE chunks are separated into list elements
#'
#' @return An object of the same class as .data.
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
sift <- function(.data, condition, variable, amount, constant, bind_rows = TRUE) {
x <- rlang::enquo(condition)
y <- rlang::enquo(variable)
z <- rlang::enquo(amount)
c <- rlang::enquo(constant)
# need to improve this so constant is not required
chunks <- .data %>%
dplyr::mutate(I = ifelse(!!x, TRUE, FALSE)) %>%
dplyr::group_by(!!c) %>%
dplyr::filter(any(I)) %>%
dplyr::group_nest() %>%
dplyr::mutate(sifted = purrr::map(data, .f = partition, variable = !!y, amount = !!z)) %>%
dplyr::select(!!c, sifted) %>%
tidyr::unnest(cols = sifted) %>%
dplyr::mutate(chunk = dplyr::group_indices(., !!c, chunk)) %>%
dplyr::arrange(!!c, !!y)
#crayon
if (bind_rows) {
chunks
} else {
chunks %>%
dplyr::group_split(chunk)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.