Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Assert that a combination of columns is unique (using `assertr`)

I am looking for a "tidy" and concise way to make sure that a combination of columns is unique in a tibble using assertr.

So far, this is the best I could come up with:

PasteRows <- function(df) {
  apply(df, 1, paste, collapse = '')
}

tib <- tibble(a = c(1, 1, 3), b = c('a', 'b', 'b'))

tib %>%
  assert_rows(PasteRows, is_uniq, a, b)

... but I first have to define PasteRows. Also, I am not sure if apply has a performance penalty, because it converts the tibble into a matrix.

How can I improve and shorten this?

like image 452
severin Avatar asked Sep 20 '25 07:09

severin


1 Answers

assertr has the function col_concat() for this purpose.

library(assertr)
library(tibble)

tib <- tibble(a = c(1, 1, 2), b = c('a', 'b', 'b'))

tib %>%
  assert_rows(col_concat, is_uniq, a, b)
like image 104
Ashby Thorpe Avatar answered Sep 22 '25 21:09

Ashby Thorpe