Validate Data Frame Columns Against Specified Validations
Source:R/validation-options.R
validateColumns.Rd
This function checks if the columns of a given data frame adhere to specified
validations defined in columnSpecs
. It validates each column's type, value range,
allowed values, and handles NULL
and NA
values based on the configurations
provided in columnSpecs
.
Arguments
- object
#' @param object A data frame, tibble, or data.table whose columns are to be validated.
- columnSpecs
A list specifying the valid configurations for each column in
object
. Each entry incolumnSpecs
should be a list containing thetype
,allowedValues
,valueRange
,nullAllowed
, andnaAllowed
parameters for the column it corresponds to.
Value
The function does not explicitly return a value. It completes without error if all columns
are valid according to columnSpecs
. If any validation fails, it generates a descriptive
error message, reporting all failures if multiple validations fail.
Details
The function iterates through each column name in columnSpecs
, retrieves the corresponding
column from object
, and validates it using validateVector
. The validation includes:
Type correctness according to
type
.Whether the column values are within the specified
valueRange
(if applicable).Whether the column values are among the
allowedValues
(if specified).Handling of
NULL
andNA
based onnullAllowed
andnaAllowed
flags.
Examples
df <- data.frame(
age = c(25L, 30L, NA),
BMI = c(22.5, 27.3, 24.9),
gender = c("M", "F", "F"),
smoker = c(TRUE, FALSE, NA)
)
columnSpecs <- list(
age = list(type = "integer", valueRange = c(18L, 65L), naAllowed = TRUE),
BMI = list(type = "numeric", valueRange = c(15.0, 40.0), naAllowed = TRUE),
gender = list(type = "character", allowedValues = c("M", "F"), naAllowed = TRUE),
smoker = list(type = "logical", naAllowed = TRUE)
)
validateColumns(df, columnSpecs)
#> NULL