matchesCriteria: Do Data Frame Row Match Given Criteria?

View source: R/logical.R

matchesCriteriaR Documentation

Do Data Frame Row Match Given Criteria?

Description

are data frame rows matching given criteria?

Usage

matchesCriteria(
  Data,
  criteria = NULL,
  na.to.false = FALSE,
  add.details = FALSE,
  dbg = TRUE
)

Arguments

Data

data frame

criteria

vector of character containing conditions, in which the column names of Data, e.g. A can appear unquoted, e.g. "A == 'x'"

na.to.false

if TRUE (the default is FALSE) NA in the resulting vector will be replaced with FALSE

add.details

if TRUE (the default is FALSE) a matrix containing the evaluation of each criterion is returned in attribute details

dbg

if TRUE (default) for each criterion in criteria it is shown for how many rows in Data the criterion is TRUE and for how many rows it is FALSE

Value

vector of logical containing TRUE at positions representing rows in Data fulfilling the conditions and FALSE elsewhere

Examples

# Define an example data frame
Data <- data.frame(A = c("x", "y", "z", NA),
                   B = c( NA,   2,   3, 4))

# Define one or more criteria
criteria <- c("A %in% c('y', 'z')", "B %in% 1:3")

# For which rows the criteria are met (vector of logical)?
matchesCriteria(Data, criteria, dbg = FALSE)

# You may use the function in the context of indexing:
Data[matchesCriteria(Data, criteria), ]

# Filtering for non-NA values
D1 <- Data[matchesCriteria(Data, "! is.na(A) & ! is.na(B)"), ]

# the same result is returned by:
D2 <- Data[matchesCriteria(Data, c("! is.na(A)", "! is.na(B)")), ]

identical(D1, D2)


KWB-R/kwb.utils documentation built on April 1, 2024, 7:12 a.m.