Filter for all instances of a column that meet a specific condition at least once.

filter_for(.data, what, where)

Arguments

.data

data frame

what

unquote col or vector of unquoted cols.

where

a logical condition used for filter

Value

data frame

Examples


# An example using some time series data
tibble::tibble( CLIENT_ID = c("A1001", "B1001", "C1001",
"A1001", "B1001", "C1001", "A1001", "B1001", "C1001"),
                YEAR = c(2019L, 2019L, 2019L, 2020L, 2020L, 2020L, 2021L, 2021L, 2021L),
                SALES = c(3124, 56424, 3214132, 65534, 2342, 6566, 87654, 2332, 6565)
) %>%
dplyr::arrange(CLIENT_ID, YEAR) -> sales_data

sales_data
#> # A tibble: 9 × 3
#>   CLIENT_ID  YEAR   SALES
#>   <chr>     <int>   <dbl>
#> 1 A1001      2019    3124
#> 2 A1001      2020   65534
#> 3 A1001      2021   87654
#> 4 B1001      2019   56424
#> 5 B1001      2020    2342
#> 6 B1001      2021    2332
#> 7 C1001      2019 3214132
#> 8 C1001      2020    6566
#> 9 C1001      2021    6565

# filter for Clients that had sales greater than 4000 in the year 2019.
# this way we can see how the same clients sales looked in subsequent years

sales_data %>%
  filter_for(what = CLIENT_ID, where = YEAR == 2019 & SALES > 4000L)
#> # A tibble: 6 × 3
#>   CLIENT_ID  YEAR   SALES
#>   <chr>     <int>   <dbl>
#> 1 B1001      2019   56424
#> 2 B1001      2020    2342
#> 3 B1001      2021    2332
#> 4 C1001      2019 3214132
#> 5 C1001      2020    6566
#> 6 C1001      2021    6565


# filter for clients whose sales were less than 4000 in the year 2021
 sales_data %>%
  filter_for(what = CLIENT_ID, where = YEAR == 2021 & SALES < 4000L)
#> # A tibble: 3 × 3
#>   CLIENT_ID  YEAR SALES
#>   <chr>     <int> <dbl>
#> 1 B1001      2019 56424
#> 2 B1001      2020  2342
#> 3 B1001      2021  2332