These helper functions allowing easy checking using an arbitrary function (func) over multiple columns (vars) of a data frame (data), with an optional filter (flt).

chk_filter(data, vars, func, flt = TRUE, args = list())

chk_filter_all(data, vars, func, flt = TRUE, args = list())

chk_filter_any(data, vars, func, flt = TRUE, args = list())

Arguments

data

A data frame to check.

vars

<tidy-select> A set of columns to check.

func

A function to use for checking that takes a vector as the first argument and returns a logical vector of the same length showing whether an element passed or failed.

flt

<data-masking> A filter specifying a subset of the data frame to test.

args

A list of additional arguments to be added to the function calls.

Value

A logical vector or data frame of logical vectors flagging records that have passed or failed the check, with NA where records do not meet the filter condition.

Details

  • chk_filter() applies func with args to vars in data filtered with flt and returns a data frame containing the resulting logical vectors.

  • chk_filter_all() and chk_filter_any() both run chk_filter() and return a single logical vector flagging whether all or any values in each row are TRUE (i.e. the conjunction and disjunction, respectively, of the columns in the output of chk_filter()).

See also

Other chk_*() functions such as chk_values()

Examples


# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches AND < 100 horsepower - return a data frame
chk_filter(
  mtcars,
  c("disp", "hp"),
  chk_range,
  cyl == 4,
  list(min = 0, max = 100)
)
#>                      disp    hp
#> Mazda RX4              NA    NA
#> Mazda RX4 Wag          NA    NA
#> Datsun 710          FALSE  TRUE
#> Hornet 4 Drive         NA    NA
#> Hornet Sportabout      NA    NA
#> Valiant                NA    NA
#> Duster 360             NA    NA
#> Merc 240D           FALSE  TRUE
#> Merc 230            FALSE  TRUE
#> Merc 280               NA    NA
#> Merc 280C              NA    NA
#> Merc 450SE             NA    NA
#> Merc 450SL             NA    NA
#> Merc 450SLC            NA    NA
#> Cadillac Fleetwood     NA    NA
#> Lincoln Continental    NA    NA
#> Chrysler Imperial      NA    NA
#> Fiat 128             TRUE  TRUE
#> Honda Civic          TRUE  TRUE
#> Toyota Corolla       TRUE  TRUE
#> Toyota Corona       FALSE  TRUE
#> Dodge Challenger       NA    NA
#> AMC Javelin            NA    NA
#> Camaro Z28             NA    NA
#> Pontiac Firebird       NA    NA
#> Fiat X1-9            TRUE  TRUE
#> Porsche 914-2       FALSE  TRUE
#> Lotus Europa         TRUE FALSE
#> Ford Pantera L         NA    NA
#> Ferrari Dino           NA    NA
#> Maserati Bora          NA    NA
#> Volvo 142E          FALSE FALSE

# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches AND < 100 horsepower
chk_filter_all(
  mtcars,
  c("disp", "hp"),
  chk_range,
  cyl == 4,
  list(min = 0, max = 100)
)
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>                  NA                  NA               FALSE                  NA 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>                  NA                  NA                  NA               FALSE 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#>               FALSE                  NA                  NA                  NA 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>                  NA                  NA                  NA                  NA 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#>                  NA                TRUE                TRUE                TRUE 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#>               FALSE                  NA                  NA                  NA 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#>                  NA                TRUE               FALSE               FALSE 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#>                  NA                  NA                  NA               FALSE 

# Check that every 4-cylinder car has an engine displacement of < 100 cubic
# inches OR < 100 horsepower
chk_filter_any(
  mtcars,
  c("disp", "hp"),
  chk_range,
  cyl == 4,
  list(min = 0, max = 100)
)
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>                  NA                  NA                TRUE                  NA 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>                  NA                  NA                  NA                TRUE 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#>                TRUE                  NA                  NA                  NA 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>                  NA                  NA                  NA                  NA 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#>                  NA                TRUE                TRUE                TRUE 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#>                TRUE                  NA                  NA                  NA 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#>                  NA                TRUE                TRUE                TRUE 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#>                  NA                  NA                  NA               FALSE 

# Check that columns made up of whole numbers are binary
chk_filter_all(
  mtcars,
  where(~ all(. %% 1 == 0)),
  chk_values,
  TRUE,
  list(0:1)
)
#>           Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive 
#>               FALSE               FALSE               FALSE               FALSE 
#>   Hornet Sportabout             Valiant          Duster 360           Merc 240D 
#>               FALSE               FALSE               FALSE               FALSE 
#>            Merc 230            Merc 280           Merc 280C          Merc 450SE 
#>               FALSE               FALSE               FALSE               FALSE 
#>          Merc 450SL         Merc 450SLC  Cadillac Fleetwood Lincoln Continental 
#>               FALSE               FALSE               FALSE               FALSE 
#>   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#>               FALSE               FALSE               FALSE               FALSE 
#>       Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28 
#>               FALSE               FALSE               FALSE               FALSE 
#>    Pontiac Firebird           Fiat X1-9       Porsche 914-2        Lotus Europa 
#>               FALSE               FALSE               FALSE               FALSE 
#>      Ford Pantera L        Ferrari Dino       Maserati Bora          Volvo 142E 
#>               FALSE               FALSE               FALSE               FALSE