Check a rowwise assertion #372
-
Thanks for this great package! I'm trying to check a rowwise assertion and have gotten stuck. I previously posted this question on SO but am moving here after several days: https://stackoverflow.com/questions/70114227/check-rowwise-assertion-in-r-pointblank I would like to check a rowwise assertion using {pointblank}. A row should "fail" if it is ALL NA; it should pass if ANY non-NA. In the reprex below, only the second row should fail. How would I write this assertion in {pointblank}? I am able to check each individual columns and whether ANY NA, but am stuck for ALL NA. Thank you for your help! library(dplyr)
library(pointblank)
df <- tribble(
~a, ~b, ~c,
1, 2, 3,
NA, NA, NA,
2, NA, 6
)
agent <-
df %>%
create_agent() %>%
# Is there an NA in each column separately?
col_vals_not_null(vars(a, b, c)) %>%
# Is there an NA in any column?
rows_complete(vars(a, b, c)) %>%
interrogate() Created on 2021-11-25 by the reprex package (v2.0.0) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
For this, I'd recommend using the library(dplyr)
library(pointblank)
df <- tribble(
~a, ~b, ~c,
1, 2, 3, # should pass (no NAs)
NA, NA, NA, # should fail (completely NAs)
2, NA, 6 # should pass (partial NAs)
)
# Create an agent and use `col_vals_expr()` to evaluate
# a more complex expression on a per-row basis
agent <-
df %>%
create_agent() %>%
col_vals_expr(expr = ~ !(is.na(a) & is.na(b) & is.na(c))) %>%
interrogate()
# This shows that only the second row fails
get_data_extracts(agent, i = 1)
#> # A tibble: 1 × 3
#> a b c
#> <dbl> <dbl> <dbl>
#> 1 NA NA NA Unfortunately, |
Beta Was this translation helpful? Give feedback.
@maia-sh
For this, I'd recommend using the
col_vals_expr()
function to create an expectation that uses an expression: