简体   繁体   中英

How to assertr dataframe based on two or more columns (R)?

I would need something like this, where the offending row is the fourth one.

library(assertr)
library(tidyverse)

tibble(
  a = c("x", "y", "z", "z"),
  b = c(1 , 2, 10, 15)
) %>% 
  assert(
    a == "z" & b == 10
  )

It is not a question of how to structure the code, but what function of assertr package to use for my goal.

Expected ouput:

Column 'b' violates assertion 'function(x) a == "z" & b == 10' 1 time
    verb redux_fn              column index value
1 assert       NA function(x)     b\a     4     x

We could use assert from checkmate

library(checkmate)
assert(testChoice("z", tbl1$a), testChoice(25, tbl1$b), combine = 'and')
#Error: Assertion on 'z' failed. FALSE.

whereas

assert(testChoice("z", tbl1$a), testChoice(15, tbl1$b), combine = 'and')

returns nothing as it is a success


According to ?assertr::assert

predicate - A function that returns FALSE when violated

We can pass only a single function but that function can be passed on to multiple columns. One option is to create a single column from the two and then pass a function

f1 <- function(x) x== 'z 10'
library(tidyr)
library(dplyr)
tbl1 %>%
   unite(ab, a, b, sep= ' ', remove = FALSE) %>%
   assertr::assert(f1, ab)
#Column 'ab' violates assertion 'f1' 3 times
#    verb redux_fn predicate column index value
#1 assert       NA        f1     ab     1   x 1
#2 assert       NA        f1     ab     2   y 2
#3 assert       NA        f1     ab     4  z 15

Or may use verify

tbl1 %>%  
  filter(a == 'z') %>%
  verify(b == 10)

data

tbl1 <- tibble(
  a = c("x", "y", "z", "z"),
  b = c(1 , 2, 10, 15)
 ) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM