Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using the %in% operator to compare values across columns #177

Open
iyedg opened this issue Nov 4, 2023 · 0 comments
Open

Using the %in% operator to compare values across columns #177

iyedg opened this issue Nov 4, 2023 · 0 comments

Comments

@iyedg
Copy link

iyedg commented Nov 4, 2023

Firstly thank you so much for creating this package, I am very thankful.

I have a situation where I want to check that values in a column a must be one of the values in columns b and c. Based on the data frame in the following reprex, I would expect rows 2, 4, and 5 to be in violation of such rule defined with the %in% operator, but that is not the case.

library(validate)

example_df <- tibble::tribble(
  ~a, ~b, ~c,
  "a", "a", "b", # a exists in b, c
  "a", "x", "z", # a does not exist in b, c
  "a", "a", NA_character_, # a exists in b, c but one of them is NA
  "a", "x", NA_character_, # a does not exist in b, c but one of them is NA,
  NA_character_, "a", "x", # a is NA but not NA in b and c
  NA_character_, NA_character_, "x" # a is na and b or c is na
)

result <- example_df |> check_that(
  a %in% c(b, c) 
)

violating(example_df, result)
#> # A tibble: 0 × 3
#> # ℹ 3 variables: a <chr>, b <chr>, c <chr>

Created on 2023-11-04 with reprex v2.0.2

I am aware that I can simulate the %in% operator with

result <- example_df |> check_that(
  a == b | a == c
)

But this would get too hard to maintain as the number of columns grows.

What is the recommended way to write such a check ?

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant