filter_by_sub: Filter _by_ nested data frames

filter_by_subR Documentation

Filter by nested data frames

Description

Applies filter to rows in a data frame based on the results of that row's nested data frame. Each logical predicate supplied to ... must evaluate to a logical of length 1, similar to summarise (which this function calls).

Usage

filter_by_sub(df, data_col_name, ..., handle_nulls = FALSE)

Arguments

df

A data frame / tibble

data_col_name

The column name of the nested data frames, bare or as a string.

...

Logical predicates defined in terms of the variables in df, that evaluate to a length of 1. Multiple conditions are combined with &. Only rows where the condition evaluates to TRUE are kept.

handle_nulls

If TRUE, drops rows with NULL data frames. Otherwise will throw an error if they are encountered.

drop_empty

If TRUE, will drop rows that, after filtering, have no rows.

Details

Unlike filter_in_sub, which applies a filter within the nested data frames, filter_by_sub applies the filter to the top-level data frame.

Value

A data frame / tibble

See Also

filter_in_sub

Examples

d <- mtcars %>%
  dplyr::mutate(Name=row.names(mtcars)) %>%
  as_tibble() %>%
  tidyr::nest(-cyl)

d %>% filter_by_sub(data, any(grepl("Merc", Name)), n() > 12)
# We can see what happens when we make a nested data frame NULL and if we make it a row of 0
d[2,]$data <- list(NULL)
d[1,]$data <- list(d[1,]$data[[1]][FALSE,])
d
## Not run: 
d %>% filter_by_sub(data, any(grepl("Merc", Name)), all(mpg < 20))

## End(Not run)
d %>% filter_by_sub(data, any(grepl("Merc", Name)), all(mpg < 20), handle_nulls = TRUE)

burchill/zplyr documentation built on Feb. 2, 2023, 11:01 a.m.