apply_filters: Apply Groups of Filter Criteria from Configuration

View source: R/apply_filters.R

apply_filtersR Documentation

Apply Groups of Filter Criteria from Configuration

Description

Apply Groups of Filter Criteria from Configuration

Usage

apply_filters(data, groups, length_column = NULL, id_columns = names(data)[1L])

Arguments

data

data frame

groups

names of filter criteria groups defined in list returned by kwb.prep:::read_filter_criteria

length_column

name of column in data containing lengths (to be summed up for the overview that is returned)

id_columns

names of column(s) in data that uniquely identify the records. This column / these columns are returned in order to report about the records that have been removed

Value

data, filtered according to the specified criteria. The returned data frame has an attribute filter_info being a list with as many elements as there are groups. The elements are named according to the values given in groups. Each list element is a list with one element overview (being a data frame with one row per filter criterion) and further elements removed_<i> being data frames with only id_columns that represent the records that have been removed in the according filter step i.

Examples


# Define filter criteria
criteria <- list(
  sepal = c(
    "sepal short" = "Sepal.Length < 5",
    "sepal narrow" = "Sepal.Width < 3"
  ),
  petal = c(
    "petal short" = "Petal.Length < 5",
    "petal narrow" = "Petal.Width < 3"
  )
)

# Write criteria to temporary yaml file
tdir <- tempdir()
yaml::write_yaml(criteria, file.path(tdir, "filter_criteria.yml"))

# Set path to temporary "config" folder so that kwb.prep knows about it
kwb.prep:::set_user_config_dir(tdir)

# Apply filter groups "sepal" and "petal" to the iris dataset
result <- apply_filters(iris, c("sepal", "petal"))

# Have a look at the result
str(result)


KWB-R/kwb.prep documentation built on June 11, 2022, 1:29 p.m.