Skip to contents

Details about criteria applied and number of rows matching each criterion is returned in the attribute "details.filter". If a criterion evaluates to NA, the corresponding row in the data frame is removed (just as if the criterion evaluated to FALSE).

Usage

applyFilterCriteria(x, criteria = NULL, lengthColumn = NULL, ...)

Arguments

x

data frame

criteria

vector of character defining filter criteria to be evaluated in x

lengthColumn

name of the column containing lengths, e.g. "Length_raw"

...

passed to matchesCriteria

Examples

# Create a very simple data frame
df <- data.frame(value = 1:10, group = rep(c("a", "b"), 5))

# Show the data frame
df
#>    value group
#> 1      1     a
#> 2      2     b
#> 3      3     a
#> 4      4     b
#> 5      5     a
#> 6      6     b
#> 7      7     a
#> 8      8     b
#> 9      9     a
#> 10    10     b

# Filter for rows meeting two criteria
result <- applyFilterCriteria(df, c(
  "value is below or equal to 5" = "value <= 5", 
  "group is 'a'" = "group == 'a'"
))
#> Evaluating value <= 5 ...
#>   is TRUE for       5 rows ( 50.0 %),
#>     FALSE for       5 rows ( 50.0 %) and
#>        NA for       0 rows (  0.0 %).
#>   Selected rows now: 5
#> Evaluating group == "a" ...
#>   is TRUE for       5 rows ( 50.0 %),
#>     FALSE for       5 rows ( 50.0 %) and
#>        NA for       0 rows (  0.0 %).
#>   Selected rows now: 3

# Show the result
result
#>   value group
#> 1     1     a
#> 3     3     a
#> 5     5     a

# Get the evaluation of each criterion in columns
kwb.utils::getAttribute(result, "matches")
#>  [1]  TRUE FALSE  TRUE FALSE  TRUE FALSE FALSE FALSE FALSE FALSE
#> attr(,"details")
#>          C1    C2
#>  [1,]  TRUE  TRUE
#>  [2,]  TRUE FALSE
#>  [3,]  TRUE  TRUE
#>  [4,]  TRUE FALSE
#>  [5,]  TRUE  TRUE
#>  [6,] FALSE FALSE
#>  [7,] FALSE  TRUE
#>  [8,] FALSE FALSE
#>  [9,] FALSE  TRUE
#> [10,] FALSE FALSE
#> attr(,"details")attr(,"criteria")
#>                              id    condition
#> value is below or equal to 5 C1   value <= 5
#> group is 'a'                 C2 group == 'a'