'Run an assert() check on a subset of the data in-line without modifying output data.frame

I want to exempt a few rows from a check with assertr::assert() without modifying the data.frame that's passed through.

For instance say I want to assert that there are no duplicated values of mtcars$qsec where mtcars$am is 0. I want to exempt the values where am = 1 and get back all of mtcars.

This fails as it should:

library(assertr)
mtcars %>%
  assert(is_uniq, qsec)

And this works but passes through the filtered data.frame:

mtcars %>% 
  filter(am == 0) %>% 
  assert(is_uniq, qsec)

What I want is this, where it would succeed and pass all the data through if there are no duplicated values of qsec where am == 0, and to throw an error if there are:

mtcars %>% 
  assert(filter(., am == 0), is_uniq, qsec)

But that doesn't work. Is there a way I can check a subset of the data in a pipeline while still getting the whole data set out at the end?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source